Home
Map
null KeywordExplore the null keyword on objects, arrays and strings. Null is not the same as zero.
C#
This page was last reviewed on Jun 27, 2021.
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#.
Nullable
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.
Exception
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.
Detail 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.
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
NullReferenceException
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.
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.
Detail 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.
string.IsNullOrEmpty
Also We can just check against the null literal. This approach is fastest if no length test is required.
null String
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.
class
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 27, 2021 (simplify).
Home
Changes
© 2007-2024 Sam Allen.