142

In C#, what is the default value of a class instance variable of type int??

For example, in the following code, what value will MyNullableInt have if it is never explicitly assigned?

class MyClass
{
    public int? MyNullableInt;
}

(It seems likely that the answer is almost certainly either null or 0, but which of those is it?)

Jogge
  • 1,654
  • 12
  • 36
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • 1
    You could have looked [here](https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx) - first google hit for me. – 500 - Internal Server Error Mar 19 '15 at 20:16
  • 3
    @500 - I did find that article when I was researching -- I actually linked to it in my own answer below -- but as far as I can tell, the article doesn't explicitly state that the default value of a nullable type instance is null. It *does* say "The default value for HasValue is false. The Value property has no default value", but that doesn't directly answer my original question without some additional digging. – Jon Schneider Mar 19 '15 at 20:19
  • 3
    I wonder if it was because you could just set a breakpoint and see what the value is, or in the Immediate Window type `int? x;` and see what the result is? – Rufus L Mar 20 '15 at 00:35
  • 1
    @RufusL I am not well versed in the details of C# but need to move quickly, so it would take an unnecessarily long amount of time for me to test and find every little behavior like this. I do not have to do that for this question thanks to Jon. – James Hurley Jul 22 '22 at 19:30
  • @JamesHurley Ok, I was just theorizing on a possible reason for a downvote to this 7 year old question. But for what it's worth, opening the immediate window, typing `int? x;` and checking the result would take less time than it took you to write that comment. – Rufus L Jul 22 '22 at 23:52
  • I'm not sure what you mean by immediate window, but if you mean writing in my open project, it was not in a buildable state--and because I looked it up, I got from the answer by Jon both the what and the why! I even had enough time to leave a comment defending him, so that I see more answers like it in the future. – James Hurley Jul 24 '22 at 00:52

3 Answers3

188

The default value for int? -- and for any nullable type that uses the "type?" declaration -- is null.

Why this is the case:

  • int? is syntactic sugar for the type Nullable<T> (where T is int), a struct. (reference)
  • The Nullable<T> type has a bool HasValue member, which when false, makes the Nullable<T> instance "act like" a null value. In particular, the Nullable<T>.Equals method is overridden to return true when a Nullable<T> with HasValue == false is compared with an actual null value.
  • From the C# Language Specification 11.3.4, a struct instance's initial default value is all of that struct's value type fields set to their default value, and all of that struct's reference type fields set to null.
  • The default value of a bool variable in C# is false (reference). Therefore, the HasValue property of a default Nullable<T> instance is false; which in turn makes that Nullable<T> instance itself act like null.
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • When I change a declaration from `int? x = null;` to `int? x;`, I get compiler errors further down in code: "Use of unassigned local variable x". – John Pick Sep 16 '16 at 17:33
  • 1
    @JohnPick That's a C# compiler feature intended to prevent bugs that might arise from you "forgetting" to initialize a variable before using it. A situation where you *can* encounter an uninitialized int? is when it's a class member variable. Examples of both here: https://blogs.msdn.microsoft.com/abhinaba/2005/11/08/c-uninitialized-instance-variables/ – Jon Schneider Sep 16 '16 at 19:06
  • 1
    So basically this place is your tech blog? You ask questions for which you just need to run the code to get the answer, then answer the question yourself in the same minute. – GôTô Nov 17 '16 at 07:38
  • 4
    @JonSchneider fair enough, my bad then – GôTô Nov 17 '16 at 15:09
  • 1
    VS's compiler hint/warning is misleading: `Field 'id' is never assigned to, and will always have its default value 0`. Weird isn't it? – Hendy Irawan Aug 26 '17 at 07:55
45

I felt important to share the Nullable<T>.GetValueOrDefault() method which is particularly handy when working with math computations that use Nullable<int> aka int? values. There are many times when you don't have to check HasValue property and you can just use GetValueOrDefault() instead.

var defaultValueOfNullableInt = default(int?);
Console.WriteLine("defaultValueOfNullableInt == {0}", (defaultValueOfNullableInt == null) ? "null" : defaultValueOfNullableInt.ToString());

var defaultValueOfInt = default(int);
Console.WriteLine("defaultValueOfInt == {0}", defaultValueOfInt);

Console.WriteLine("defaultValueOfNullableInt.GetValueOrDefault == {0}", defaultValueOfNullableInt.GetValueOrDefault());

Command line showing the code above working

Community
  • 1
  • 1
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
-1
var x = default (int?);
Console.WriteLine("x == {0}", (x == null) ? "null" : x.ToString());
Rufus L
  • 36,127
  • 5
  • 30
  • 43