Lately while going through the code of latest MEF release, I stumbled upon a piece of code that used a very little known feature of .NET
Just take a look:
public struct Tuple<TFirst, TSecond>
public Tuple(TFirst first, TSecond second)
this = new Tuple<TFirst, TSecond>();//looks strange?
this.First = first;
this.Second = second;
I myself was shocked at first. “Hey! You can’t assign to this! It’s against the nature and common sense.” Then I went to C# language specification to look for explanation and indeed I’ve found it.
The key here is the fact that Tuple is a struct, not a class. As it turns out, the meaning of this differs between those two.
The explanation comes from chapter 11.3.6 – Meaning of this
Within an instance constructor or instance function member of a class, this is classified as a value. Thus, while this can be used to refer to the instance for which the function member was invoked, it is not possible to assign to this in a function member of a class.
Within an instance constructor of a struct, this corresponds to an out parameter of the struct type, and within an instance function member of a struct, this corresponds to a ref parameter of the struct type. In both cases, this is classified as a variable, and it is possible to modify the entire struct for which the function member was invoked by assigning to this or by passing this as a ref or out parameter.
It makes total sense when you think about it, still – I think many experienced people can be surprised by that.