In the previous tutorial, We learnt about some common operators used in C#
In this tutorial,We are going to learn about Nullable types in C#
As we already discussed about the buit-in types of C#.The built-in types in C# are classified into two categories. They are
1. Value Types
2. Reference Types
When we create a value type, C# compiler will allocate space in memory to store that value. All Primitive types such as int,float,bool and char are also value types. All the value types will store in stack portion of the memory.By default Compiler allocates some value in the memory. We cannot store null values
in value types in C#.
When we create a Reference type, C# compiler allocates some space in Heap segment of the memory. Reference types consists of interfaces, class, delegates, arrays etc.By default the value stored in the reference types in
Difference between Value and Reference types
Value types will store in stack segment of the memory, So value types are faster when compared to reference types. The default value in value types wil be some constant, whereas in reference types the default value is null.If we store null type of value in value types the C# compiler will throw an error.So C# allows us to create nullable types so that we can store null value in value types.
Creating Nullable Types
The syntax for value types is
Eg: int i =null (compiler throws error)
int? i=null(no error)
The ?(Question mark) symbol after the data type will tell the compiler that the created variable is of nullable type.Nullable types bridge the difference between C# types and database types.