• Register
100 points

Nullable types bridge the differences between C# types and Database types. In C#, there are two broad categories for types,  value types such as int, float, double, structs, enums etc. and referance types such as Interface, Class, delegates, arrays etc. By default all value types are non-nullable.

Make a field of value type nullabe

let an iteger type variable declared with identifier i.

int i = 0;

Hence type of i is int which is non-nullable, so cannot be set to null, if the value of i becomes null will generate compiler error. To make this field nullable use ‘?’ symbol immediate after of type name.

int? i = null;

Now it is a nullabe integer and legally the value of i can be null.

Code Example

using System;

class Program
    static void Main()
        int inStock;
        int? itemsOnSale = null;

        //Handle dependent fields with null coalesce operator ??
        inStock = itemsOnSale ?? 0;

        Console.WriteLine("Available items: {0}", inStock);


Available items: 0


100 points