Search code examples
c#.netdeclarationnullable

Difference between declaration


What is the difference between these two codes :

public uint? a;
public uint a;

I want to know what is the difference if we use ? after int or double or the others

Thanks in advance


Solution

  • uint? is a nullable type, it can hold null value, for example.

    uint? a;
    uint b;
    
    a = null;
    b = null; //this will cause error
    

    in C# 4.0 you can check using null coalescing operator

        b = a ?? 0;
    

    its same as

        if (a.HasValue)
            b = a;
        else
            b = 0;