int value=0;
if (value == 0)
{
value = null;
}
How can I set value
to null
above?
Any help will be appreciated.
int value=0;
if (value == 0)
{
value = null;
}
How can I set value
to null
above?
Any help will be appreciated.
You cannot set an
int
tonull
. Use a nullable int (int?
) instead:Additionally, you cannot use "null" as a value in a conditional assignment. e.g...
FAILS with:
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '<null>'.
So, you have to cast the null as well... This works:
int does not allow null, use-
or use
Use Null.NullInteger ex: private int _ReservationID = Null.NullInteger;
In .Net, you cannot assign a
null
value to anint
or any other struct. Instead, use aNullable<int>
, orint?
for short:Further Reading
OR
or
or
or just
The null keyword is a literal that represents a null reference, one that does not refer to any object. In programming, nullable types are a feature of the type system of some programming languages which allow the value to be set to the special value NULL instead of the usual possible values of the data type.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null https://en.wikipedia.org/wiki/Null