89
int value=0;

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

How can I set value to null above?

Any help will be appreciated.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
user2902180
  • 1,021
  • 3
  • 12
  • 12
  • possible duplicate of [Why type "int" is never equal to 'null'?](http://stackoverflow.com/questions/6191339/why-type-int-is-never-equal-to-null) – Sergey Berezovskiy Oct 22 '13 at 15:36
  • 1
    Note that 'value' is a keyword in C#. This code presented is legal C#, but probably a bad idea to use. http://msdn.microsoft.com/en-us/library/vstudio/a1khb4f8.aspx – Aric TenEyck Oct 22 '13 at 15:38
  • @Aric TenEyck Not saying it makes it right, but some framework method parameters have the name `value`. For example, [`Enum.Parse`](http://msdn.microsoft.com/library/essfb559.aspx), [`String.IndexOf`](http://msdn.microsoft.com/library/k8b1470s.aspx) and (logically) [`Dictionary<,>.TryGetValue`](http://msdn.microsoft.com/library/bb347013.aspx). – Lance U. Matthews Oct 22 '13 at 16:04

6 Answers6

127

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

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • I was trying to assign null from immediate window for debugging purpose. It didn't work though. How can this be achieved? – Mahendran Dec 26 '18 at 12:10
  • @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?`. – p.s.w.g Dec 27 '18 at 16:19
  • the variable is declared `nullable`. Yet in immediate window, I couldn't assign null. – Mahendran Dec 29 '18 at 04:30
  • cannot assign null value to "int?", getting System.InvalidOperationException – Kasim Husaini Jun 08 '23 at 05:41
123

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.

doublehelix
  • 2,302
  • 2
  • 18
  • 15
  • 7
    Excellent. Solved an issue I was having tonight. – Ron Apr 13 '16 at 21:02
  • 2
    Ok, this is not strictly the answer to the question but I find it really useful. Anyone know the reason of this behaviour? – Cirelli94 Oct 26 '17 at 08:29
  • 1
    @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` (shorthand `int?`) then the implicit cast of `int` to `int?` is found and used for the final assignment. – Jesse Chisholm Oct 20 '21 at 01:20
16

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

int? value = null;
Jon
  • 428,835
  • 81
  • 738
  • 806
2

int does not allow null, use-

int? value = 0  

or use

Nullable<int> value
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
A J
  • 107
  • 7
2
 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://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null https://en.wikipedia.org/wiki/Null

Amadeu Antunes
  • 678
  • 1
  • 8
  • 28
0

Declare you integer variable as nullable eg: int? variable=0; variable=null;