Class This example shows that when a class-level variable is used in any method, it is null if it has not been initialized yet.
Note The compiler allows the code to be executed. This is a pattern that many programs use.
Info A compile-time error will occur if you not assign the string. This is called definite assignment analysis.
Tip The example shows that you can use unassigned member variable strings, but not unassigned local variable strings.
static string _test1;
static void Main()
// Instance strings are initialized to null.if (_test1 == null)
Console.WriteLine("String is null");
// Use of unassigned local variable 'test2'
if (test2 == null)
}String is null
Length. One common problem with null strings is trying to take their lengths with the Length property. This will cause a NullReferenceException.