null (C#-Referenz)

Aktualisiert: Juli 2008

Bei dem null-Schlüsselwort handelt es sich um ein Literal, das einen Nullverweis repräsentiert, also einen Verweis, der sich auf kein Objekt bezieht. Hierbei ist null der Standardwert für Variablen des Referenztyps. Reguläre Werttypen können nicht NULL sein. In C# 2.0 wurden jedoch auf NULL festlegbare Werttypen eingeführt. Siehe Typen, die NULL-Werte zulassen (C#-Programmierhandbuch).

Im folgenden Beispiel werden einige Verhaltensweisen des NULL-Schlüsselworts veranschaulicht:

class Program
{
    class MyClass
    {
        public void MyMethod() { }
    }

    static void Main(string[] args)
    {
        // Set a breakpoint here to see that mc = null.
        // However, the compiler considers it "unassigned."
        // and generates a compiler error if you try to
        // use the variable.
        MyClass mc;

        // Now the variable can be used, but...
        mc = null;

        // ... a method call on a null object raises 
        // a run-time NullReferenceException.
        // Uncomment the following line to see for yourself.
        // mc.MyMethod();

        // Now mc has a value.
        mc = new MyClass();

        // You can call its method.
        mc.MyMethod();

        // Set mc to null again. The object it referenced
        // is no longer accsessible and can now be garbage-collected.
        mc = null;

        // A null string is not the same as an empty string.
        string s = null;
        string t = String.Empty; // Logically the same as ""

        // Equals applied to any null object returns false.
        bool b = (t.Equals(s));
        Console.WriteLine(b);

        // Equality operator also returns false when one
        // operand is null.
        Console.WriteLine("Empty string {0} null string", s == t ? "equals": "does not equal");

        // Returns true.
        Console.WriteLine("null == null is {0}", null == null);


        // A value type cannot be null
        // int i = null; // Compiler error!

        // Use a nullable value type instead:
        int? i = null;

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}

C#-Programmiersprachenspezifikation

Weitere Informationen finden Sie in den folgenden Abschnitten von C#-Programmiersprachenspezifikation:

  • 2.4.4.6 Das null-Literal

Siehe auch

Konzepte

C#-Programmierhandbuch

Referenz

C#-Schlüsselwörter

Literalschlüsselwörter (C#-Referenz)

Weitere Ressourcen

C#-Referenz

Tabelle für Standardwerte (C#-Referenz)

Änderungsverlauf

Date

Versionsgeschichte

Grund

Juli 2008

Hinzugefügtes Codebeispiel.

Korrektur inhaltlicher Fehler.