visit
Note. If you are interested in nullable reference types, you can read several articles by my colleagues: "", "".
Take a look at the sample code below and answer what will be output to the console. And, just as importantly, why. Just let's agree right away that you will answer as it is: without compiler hints, documentation, reading literature, or anything like that. :)1. Assume that int? is a reference type.
Let's reason, that int? is a reference type. In this case, null will be stored in a, and it will also be stored in aObj after assignment. A reference to an object will be stored in b. It will also be stored in bObj after assignment. As a result, Object.ReferenceEquals will take null and a non-null reference to the object as arguments, so...
That needs no saying, the answer is False!
2. Assume that int? is a value type.
Or maybe you doubt that int? is a reference type? And you are sure of this, despite the int? a = null expression? Well, let's go from the other side and start from the fact that int? is a value type.
In this case, the expression int? a = null looks a bit strange, but let's assume that C# got some extra syntactic sugar. Turns out, a stores an object. So does b. When initializing aObj and bObj variables, objects stored in a and b will be boxed, resulting in different references being stored in aObj and bObj. So, in the end, Object.ReferenceEquals takes references to different objects as arguments, therefore...
That needs no saying, the answer is False!
3. We assume that here we use Nullable<T>.
Let's say you didn't like the options above. Because you know perfectly well that there is no int?, but there is a value type Nullable<T>, and in this case Nullable<int> will be used. You also realize that a and b will actually have the same objects. With that, you remember that storing values in aObj and bObj will result in boxing. At long last, we'll get references to different objects. Since Object.ReferenceEquals gets references to the different objects...
That needs no saying, the answer is False!
4. ;)
For those who started from value types - if a suspicion crept into your mind about comparing links, you can view the documentation for Object.ReferenceEquals at . In particular, it also touches on the topic of value types and boxing/unboxing. Except for the fact that it describes the case, when instances of value types are passed directly to the method, whereas we made the boxing separately, but the main point is the same.
When comparing value types, if objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method. This means that if both objA and objB represent the same instance of a value type, the ReferenceEquals method nevertheless returns false, as the following example shows.
Here we could have ended the article, but the thing is that... the correct answer is True.
Well, let's figure it out.Simple way
int? is Nullable<int>. Open , where we look at the section "Boxing and Unboxing". Well, that's all, see the behavior description. But if you want more details, welcome to the interesting path. ;)
Interesting way
There won't be enough documentation on this path. It describes the behavior, but does not answer the question 'why'?What are actually int? and null in the given context? Why does it work like this? Are there different commands used in the IL code or not? Is behavior different at the CLR level? Is it another kind of magic?
Let's start by analyzing the int? entity to recall the basics, and gradually get to the initial case analysis. Since C# is a rather "sugary" language, we will sometimes refer to the IL code to get to the bottom of things (yes, C# documentation is not our cup of tea today).
int?, Nullable<T>Here we will look at the basics of nullable value types in general: what they are, what they are compiled into in IL, etc. The answer to the question from the case at the very beginning of the article is discussed in the next section.Let's look at the following code fragment:We're done with the default initialization - we saw the related IL code above. What happens here when we want to initialize aVal with the value 62?
Look at the IL code:Again, nothing complicated - the aVal address pushes onto the evaluation stack, as well as the value 62. After the constructor with the signature Nullable<T>(T) is called. In other words, the following two statements will be completely identical:
As you may have guessed, there is actually no null - all that happens is accessing the Nullable<T>.HasValue property. In other words, the same logic in C# can be written more explicitly in terms of the entities used, as follows.
The source code of the Nullable<T> type can be viewed, for example, on GitHub in the dotnet/runtime repository - a . There's not much code there, so check it out just for kicks. From there, you can learn (or recall) the following facts.
For convenience, the Nullable<T> type defines:
Nullable<T> boxing
Let me remind you that when boxing an object of a value type, a new object will be created on the heap. The following code snippet illustrates this behavior:The result of comparing references is expected to be false. It is due to 2 boxing operations and creating of 2 objects whose references were stored in obj1 and obj2
Now let's change int to Nullable<int>.
The result is expectedly false.
And now, instead of 62, we write the default value.Aaand... the result is unexpectedly true. One might wonder that we have all the same 2 boxing operations, two created objects and references to two different objects, but the result is true!
Yeah, it's probably sugar again, and something has changed at the IL code level! Let's see.Example N1.C# code:As we can see, in all cases boxing happens in the same way - values of local variables are pushed onto the evaluation stack (ldloc instruction). After that the boxing itself occurs by calling the box command, which specifies what type we will be boxing.
Next we refer to , see the description of the box command, and find an interesting note regarding nullable types:
If typeTok is a value type, the box instruction converts val to its boxed form. ... If it is a nullable type, this is done by inspecting val's HasValue property; if it is false, a null reference is pushed onto the stack; otherwise, the result of boxing val's Value property is pushed onto the stack.
This leads to several conclusions that dot the 'i':Let's go back to the examples with Nullable<T> that we touched upon above.
First:The value 62 is boxed twice. As we remember, in this case, instances of the int type are boxed, not Nullable<int>. Then 2 new objects are created, and 2 references to different objects are obtained, the result of their comparing is false.
Second:Since is hasValue is false, objects are not created. The boxing operation returns null which is stored in variables obj1 and obj2. Comparing these values is expected to return true.
In the original example, which was at the very beginning of the article, exactly the same thing happens:For the sake of interest, let's look at the CoreCLR source code from the repository mentioned earlier. We are interested in the file , specifically, the Nullable::Box method with the logic we need:
Here we have everything we discussed earlier. If we don't store the value, we return NULL:
P.S. Someone might have a question: how did we happen to dig that deep in this topic? We were writing a new diagnostic rule in related to Object.ReferenceEquals working with arguments, one of which is represented by a value type. Suddenly it turned out that with Nullable<T> there is an unexpected subtlety in the behavior when boxing. We looked at the IL code - there was nothing special about the box. Checked out the CLI specification - and gotcha! The case promised to be rather exceptional and noteworthy, so here's the article right in front of you.
P.P.S. By the way, recently, I have been spending more time on Twitter where I post some interesting code snippets and retweet some news in the .Net world and so on. Feel free to look through it and follow me if you want ().
Previously published at