42
int n == 0;

if (n == null)    
{  
    Console.WriteLine("......");  
}

Is it true that the result of expression (n == null) is always false since
a   value of type int  is never equal to   null of type int? (see warning below)

Warning CS0472 The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'

6

8 Answers 8

92

If you want your integer variable to allow null values, declare it to be a nullable type:

int? n = 0;

Note the ? after int, which means that type can have the value null. Nullable types were introduced with v2.0 of the .NET Framework.

4
  • Alternatively (though I do like the shorthand ? post-fix better) there is Nullable<T>. -- EDIT: Additionally, you can now reference either (in this example) n.Value or use short-hand int m = n ?? default(int); (or whatever you chose to be the default value) [syntactical sugar for int m = (n.HasValue ? n.Value : default(int));] May 31, 2011 at 17:41
  • 4
    @Brad true—I would be wasting much of my life if I did not use the ? shorthand. May 31, 2011 at 17:44
  • @Brad: there's n.GetValueOrDefault() as well instead of using ??
    – thecoop
    Oct 7, 2011 at 19:14
  • @thecoop - yes. However, the difference is, that ?? is usually shorter, e.g. n ?? int.MaxValue or n ?? int.MinValue - or, if it makes sense in your case n ?? 0. The same would be for example n.GetValueOrDefault(int.MaxValue). This function comes from an older .NET version, the ?? operator was introduced later, also the ? for nullable types as in int?, which is equivalent to Nullable<int>.
    – Matt
    Oct 18, 2017 at 10:34
19

In C# using an uninitialized variable is not allowed.

So

int i;
Console.Writeline(i);

Results in a compilation error.

You can initialize int with new such as:

int anInt = new int();

This will result in the Default value for int which is 0. In cases where you do wish to have a generic int one can make the int nullable with the syntax

int? nullableInt = null;
3
  • Why the downvote? Its important to point out how initialization works in terms of nullability. No other answer did this. Especially considering the ambiguity of the question. We need to ensure all ground is covered for the OP and future people who have this general question. May 31, 2011 at 18:14
  • I downvoted because initialization wasn't even an issue (though, oddly, the OP's initialization won't compile). At the time, that's what I perceived to be the point of your answer, and I thought it off-topic. My vote appears to be locked in, however. Jun 1, 2011 at 4:08
  • 1
    @Jeol B Fant - I noticed that the OP initialization won't compile. That's what made me think of posting this answer explaining initialization and nulls in regards to the C# lang. Jun 1, 2011 at 13:25
6

Because int is a value type rather than a reference type. The C# Language Specification doesn't allow an int to contain null. Try compiling this statement:

int x = null ;

and see what you get.

You get the compiler warning because it's a pointless test and the compiler knows it.

4

"Value types" in .NET (like int, double, and bool) cannot, by definition, be null - they always have an actual value assigned. Check out this good intro to value types vs. reference types.

2

The usage of NULL applies to Pointers and References in general. A value 0 assigned to an integer is not null. However if you can assign a pointer to the integer and assign it to NULL, the statement is valid.

To sum up =>

 /*Use the keyword 'null' while assigning it to pointers and references. Use 0 for integers.*/
2

Very simply put, an int is a very basic item. It's small and simple so that it can be handled quickly. It's handled as the value directly, not along the object/pointer model. As such, there's no legal "NULL" value for it to have. It simply contains what it contains. 0 means a 0. Unlike a pointer, where it being 0 would be NULL. An object storing a 0 would have a non-zero pointer still.

If you get the chance, take the time to do some old-school C or assembly work, it'll become much clearer.

2
 public static int? n { get; set; } = null;

OR

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

or

 public static int? n = null;

or

 public static int? n

or just

 public static int? n { get; set; } 


    static void Main(string[] args)
    {


    Console.WriteLine(n == null);

     //you also can check using 
     Console.WriteLine(n.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

1

No, because int is a value type. int is a Value type like Date, double, etc. So there is no way to assigned a null value.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.