47

I've this comparison which equals false as expected

bool eq = typeof(int?).Equals(typeof(int));

now I have this code

List<object> items = new List<object>() { (int?)123 };
int result = items.OfType<int>().FirstOrDefault();

but this returns 123 - anyway that value is of type int?

How can this be?

Impostor
  • 2,080
  • 22
  • 43
  • `int?` boxed as `int` , and basically every Nullable type, **Edit** : Marc Gravell have the full answer – styx Mar 27 '19 at 08:30
  • Related post about nullable type: https://stackoverflow.com/questions/4028830/nullableint-vs-int-is-there-any-difference. This is called as "type lifting". – Tetsuya Yamamoto Mar 27 '19 at 08:32
  • Before reading this topic I wouldn't even guess that even `List` already holds just `int` types. [Proof](https://dotnetfiddle.net/pNKb6D) – Sinatr Mar 27 '19 at 08:46
  • 7
    @Sinatr no, that is incorrect; `List` holds `int?`. The important distinction in this example is the use of `List`. What you're seeing in that "proof" is something very different; `GetType()` on any `T?` either returns the `T`, or throws a NRE. It **never** returns `T?` - better example: https://dotnetfiddle.net/3Gy3Fa - and as for why: because `GetType()` is non-virtual, it cannot be overridden, and thus calling `GetType()` **is a boxing operation** (even if used via "constrained call"). And when you box a `T?`, you either get a `T` as an `object`, or a `null`. – Marc Gravell Mar 27 '19 at 08:49
  • @TetsuyaYamamoto That is not a relevant link. [How is the boxing/unboxing behavior of Nullable possible?](https://stackoverflow.com/q/3775582/11683) is. – GSerg Mar 27 '19 at 11:17
  • What do you think happens if you have a `null` value in your `items` list? – Tvde1 Mar 27 '19 at 13:39
  • @MarcGravell Why does `OfType().First()` return a value from a `List` though? Is there any boxing involved in this case? – 41686d6564 stands w. Palestine Mar 27 '19 at 21:04

3 Answers3

60

Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null (regular null, no type), or as the non-nullable type (the T in T?). So: an int? is boxed as an int, not an int?. Then when you use OfType<int>() on it, you get all the values that are int, which is: the single value you passed in, since it is of type int.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • phew ok thank you for that explanaion. Is that C# basic knowledge? – Impostor Mar 27 '19 at 08:30
  • 14
    @Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages) – Marc Gravell Mar 27 '19 at 08:31
  • How would the query to obtain only `int?` from above `items` list will look like? – Sinatr Mar 27 '19 at 08:32
  • 5
    @Sinatr you can't - the list never contains `int?` - it only contains `int` *because of* the boxing rules on nullable types – Marc Gravell Mar 27 '19 at 08:34
  • Maybe I'm missing something but it returning 123 makes perfect sense to be. This long explanation does not. You added an int to a list and said give me all the integers in the list. The ? makes it so you can pass a null in the place of an int. – Kyle Johnson Mar 27 '19 at 19:17
  • 1
    @KyleJohnson you added a *nullable* int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently. – mbrig Mar 27 '19 at 21:24
  • 1
    (and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening) – mbrig Mar 27 '19 at 21:26
9

A nullable value type is boxed by the following rules

  • If HasValue returns false, the null reference is produced.
  • If HasValue returns true, a value of the underlying value type T is boxed, not the instance of nullable.

In your example second rule has been followed as you have value:

var i = (object)(int?)123;
Johnny
  • 8,939
  • 2
  • 28
  • 33
4

It is a bit late, but beside of Marc's answer to your question, I want to give some additional information about Nullable value types in CLR.

The CLR has built-in support for nullable value types. This special support is provided for boxing, unboxing, calling GetType, calling interface methods.

For example, let's check GetType():

Int32? x = 5;
Console.WriteLine(x.GetType());

What you think it will print to the console? System.Nullable<Int32? Not, the result is System.Int32.

Or, let's check boxing, which you noted in your question:

Int32? n =5;
Object o = n;
Console.WriteLine("o's type={0}", o.GetType()); // "System.Int32"

The rule is that:

When the CLR is boxing a Nullable instance, it checks to see if it is null, and if so, the CLR doesn’t actually box anything, and null is returned. If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words, a Nullable with a value of 5 is boxed into a boxed-Int32 with a value of 5.

And, at the end I want to explain how CLR add special support for calling interface methods from Nullable Types. Let's take a look to that:

Int32? n = 5;
Int32 result = ((IComparable) n).CompareTo(5); // Compiles & runs OK
Console.WriteLine(result); // 0

In the preceding code, I’m casting n, a Nullable<Int32>, to IComparable<Int32>, an interface type. However, the Nullable<T> type does not implement the IComparable<Int32> interface as Int32 does. The C# compiler allows this code to compile anyway.

Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • So, nullables have special treatment by the compiler? I couldn't implement these rules for my own type? – markonius Apr 05 '19 at 08:37
  • 1
    @Markonius For example you can not override `GetType` for your own type to lie about the real type of your object. Because, `GetType` is non-virtual. – Farhad Jabiyev Apr 05 '19 at 08:41