How to set null value to int in c#?

269,119

Solution 1

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

Solution 2

Additionally, you cannot use "null" as a value in a conditional assignment. e.g...

bool testvalue = false;
int? myint = (testvalue == true) ? 1234 : null;

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? myint = (testvalue == true) ? 1234 : (int?)null;

UPDATE (Oct 2021):

As of C# 9.0 you can use "Target-Typed" conditional expresssions, and the example will now work as c# 9 can pre-determine the result type by evaluating the expression at compile-time.

Solution 3

You cannot set an int to null. Use a nullable int (int?) instead:

int? value = null;

Solution 4

int does not allow null, use-

int? value = 0  

or use

Nullable<int> value

Solution 5

 public static int? Timesaday { get; set; } = null;

OR

 public static Nullable<int> Timesaday { get; set; }

or

 public static int? Timesaday = null;

or

 public static int? Timesaday

or just

 public static int? Timesaday { get; set; } 


    static void Main(string[] args)
    {


    Console.WriteLine(Timesaday == null);

     //you also can check using 
     Console.WriteLine(Timesaday.HasValue);

        Console.ReadKey();
    }

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

Share:
269,119

Related videos on Youtube

user2902180
Author by

user2902180

Updated on October 21, 2021

Comments

  • user2902180
    user2902180 over 2 years
    int value=0;
    
    if (value == 0)
    {
        value = null;
    }
    

    How can I set value to null above?

    Any help will be appreciated.

  • Ron
    Ron about 8 years
    Excellent. Solved an issue I was having tonight.
  • Cirelli94
    Cirelli94 over 6 years
    Ok, this is not strictly the answer to the question but I find it really useful. Anyone know the reason of this behaviour?
  • Mahendran
    Mahendran over 5 years
    I was trying to assign null from immediate window for debugging purpose. It didn't work though. How can this be achieved?
  • p.s.w.g
    p.s.w.g over 5 years
    @mahemadhi probably for the same reason you can't do it in regular code. If a variable is declared as an int, it cannot receive null as a value, even while debugging. You need to declare it as an int?.
  • Mahendran
    Mahendran over 5 years
    the variable is declared nullable. Yet in immediate window, I couldn't assign null.
  • Jesse Chisholm
    Jesse Chisholm over 2 years
    @Cirelli94 The ternary operator ?: tries to deduce the type of the result. It gets int for the first part and null for the second part. It can't implicitly resolve those two. But, when casting the null to be a Nullable<int> (shorthand int?) then the implicit cast of int to int? is found and used for the final assignment.