How to set null value to int in c#?

user2902180 picture user2902180 · Oct 22, 2013 · Viewed 225.7k times · Source
int value=0;

if (value == 0)
{
    value = null;
}

How can I set value to null above?

Any help will be appreciated.

Answer

p.s.w.g picture p.s.w.g · Oct 22, 2013

In .Net, you cannot assign a null value to an int or any other struct. Instead, use a Nullable<int>, or int? for short:

int? value = 0;

if (value == 0)
{
    value = null;
}

Further Reading