TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Nullable Int

This C# program uses a nullable int. Like all nullable types, a nullable int can be assigned to null.

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.

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 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.

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.

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.

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf