C# Nullable Int – How to Set Null Value to int in C#

.netc++intnullnullable

int value=0;

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

How can I set value to null above?

Any help will be appreciated.

Best Answer

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

Related Question