.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple


C#: .NET: Struct
Question

Nullable Int. A nullable int can store null. Nullable types are constructed by specifying the question mark after a value type in a declarator statement. The nullable int can be specified with the syntax "int?".

Int

Example. First, this example program demonstrates a use of the nullable integer type. This type is specified with the "int?" syntax. You can use a question mark at the end of a value type to transform the type into a nullable type.

Null-keyword

Here: We see a null int type, the HasValue and Value properties. We use the nullable type to acquire the value of the instance.

Based on:

.NET 4.5

C# program that uses nullable int type

using System;

class Program
{
    static void Main()
    {
	//
	// Create a local variable of type nullable integer.
	// ... It is initially assigned to null.
	// ... The HasValue property is false.
	//
	int? value = null;
	Console.WriteLine(value.HasValue);
	//
	// Assign the nullable integer to a constant integer.
	// ... The HasValue property is now true.
	// ... You can access the Value property as well.
	//
	value = 1;
	Console.WriteLine(value.HasValue);
	Console.WriteLine(value.Value);
	Console.WriteLine(value);
	if (value == 1)
	{
	    Console.WriteLine("True");
	}
    }
}

Output

False
True
1
1
True
Question

Question mark suffix. The Main entry point uses a statement list in its method body that includes a local variable of type nullable int. Nullable integers are specified with the "int?" type.

Also: The int type is actually aliased to the System.Int32 type, and using "System.Int32?" would work as well.

Tip: When you use a type such as int?, you have access to extra properties such as HasValue and Value.

Int

The HasValue property returns a bool that indicates whether the nullable instance contains a set value. If the type is null, it does not have a value and HasValue is false. If the type is assigned to an integer, HasValue is true.

Also: If the HasValue property is true, you can access the Value property without an exception.


Framework: NET

Implementation. Let's examine the implementation of the nullable generic struct in the base class library. When you use a type such as int?, the C# compiler actually uses the Nullable<T> struct, where T is the value type you are using such as int.

Tip: Structs in the C# language are allocated in continuous memory for performance reasons, and this makes nullable types fairly efficient.

However: There is overhead to using nullable types and this translates to reduced performance over raw value types in some cases.


C# language

Summary. Nullable types are described in the C# language specification as being value types that are wrapped inside the nullable type. Nullable ints can be useful when you want to add another state (invalid or uninitialized) to a value type.