C# null KeywordExplore the null keyword on objects, arrays and strings. Null is not the same as zero.
Null. This keyword means "no object." Here we see some information and facts about the null literal. There are some special rules with null in C#.
Null is represented by the value 0, but you cannot use 0 instead of null in your programs. Null causes lots of exceptions. We use if-statements to prevent these problems.
First example. This program assigns an object reference to the null literal. You can use null with any reference type. This includes strings, arrays and custom types.
Program We see how the intermediate language represents the null literal (with ldnull).
Info Null is not the same thing as the constant zero. The compiler makes sure the two values are not used in inappropriate contexts.
C# program that assigns reference to null
using System; class Program { static void Main() { object value = new object(); value = null; Console.WriteLine(value); } }
.method private hidebysig static void Main() cil managed { .entrypoint .maxstack 1 .locals init ( [0] object 'value') L_0000: newobj instance void [mscorlib]System.Object::.ctor() L_0005: stloc.0 L_0006: ldnull L_0007: stloc.0 L_0008: ldloc.0 L_0009: call void [mscorlib]System.Console::WriteLine(object) L_000e: ret }
Arrays. You can assign an array reference to the null literal. When you then try to access a property or method on that null reference, you get a NullReferenceException.
Null Array
C# program that assigns null to array
using System; class Program { static void Main() { string[] array = { "a", "b", "c" }; array = null; int value = array.Length; Console.WriteLine(value); } }
(Output was truncated.) Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Program.Main() in C:\Users\...\Program.cs:line 9
Arrays, continued. We show a program that checks the array against the null literal and avoids the exception. The program completes without throwing an exception.
C# program that assigns null to array, no exception
using System; class Program { static void Main() { string[] array = { "a", "b", "c" }; array = null; if (array != null) { int value = array.Length; Console.WriteLine(value); } } }
(It doesn't crash.)
A question. What happens when you assign a reference to the null literal? There is no null object allocation. Instead, the value zero is copied to the reference.
Assignments In the C# language, all assignments are simple bitwise copies. No allocations occur.
Tip Assignments are extremely fast to execute. They need no custom optimization.
Strings. Because strings are a reference type, they too can be null. We often want to protect against null strings. We do this with string.IsNullOrEmpty or string.IsNullOrWhiteSpace.
IsNullOrEmpty, IsNullOrWhiteSpace
Also We can just check against the null literal. This approach is fastest if no length test is required.
Null Strings
Fields. Fields that are reference types are always initialized to null when the enclosing type is instantiated. We therefore never have to initialize fields to null in a class constructor.
Some patterns encourage the elimination of null things. For example we can use a Null Object that is not actually null, but is treated in a similar way. This can improve some code.
A summary. Is nothing (null) something? This question is hard to answer. Null is an important concept in imperative languages. We discovered some low-level machine details about null.
© 2007-2021 sam allen. see site info on the changelog