Reputation: 2237
What is type identity in the context of the C# generic types?
I am going through the chapters of the CLR via C# book. And I come across the concept of the Generic Type Identity.
It seems that it should be clear for everyone reading the chapter what is the type identity. But I do not know what it is.
In the book there is the example:
internal sealed class DateTimeList : List<DateTime> {
// No need to put any code in here!
}
...you lose type identity and equivalence, as you can see in the following code:
Boolean sameType = (typeof(List<DateTime>) == typeof(DateTimeList));
From that example I can clearly see what the type equivalence is (it is the equality of the type objects). While I can not get what is referred to by the type identity.
So, could someone explain the type identity concept, please?
Upvotes: 2
Views: 531
Reputation: 26665
There is nothing special and author is not used that word only in the context of Generic types.
When you are at first time using some type, then instance of that type is created on the heap and it contains type related information. I guess you are already aware that, every object on the heap requires some additional members. And one of them is called type object pointer. And that pointer points to the corresponding Type
object which is stored on the heap. By Type Identity, author means that whether references to the Type
objects are pointing to the same object in the heap or not.
In that case, identities of the types are different, because objects which has been get with the help of the typeof
are different objects which are stored in the heap. Type
objects of DateTimeList
and List<DateTime>
are two different objects. That's why sameType
is false.
internal sealed class DateTimeList : List<DateTime> {
// No need to put any code in here!
}
But, in that case identities of the types are same. Currently, DateTimeList
is just an alias. As the code compiles, the compiler will replace all DateTimeList
with List<System.DateTime
.
using DateTimeList = System.Collections.Generic.List<System.DateTime>;
For example, in that case
Boolean sameType = (typeof(List<DateTime>) == typeof(DateTimeList));
will be replaced by compiler as:
// obvious that, the result is true
Boolean sameType = (typeof(List<DateTime>) == typeof(List<DateTime));
By the way, Author used Type Identity
in scope of that code-block as well:
// Define a type, construct an instance of it, & initialize its properties
var o1 = new { Name = "Jeff", Year = 1964 };
var people = new[] {
o1,
new { Name = "Kristin", Year = 1970 },
new { Name = "Aidan", Year = 2003 },
new { Name = "Grant", Year = 2008 }
};
And he mentioned that:
This top line of code (
o1
) creates an anonymous type because I did not specify a type name after the new keyword, so the compiler will create a type name for me automatically and not tell me what it is (which is why it is called an anonymous type).The compiler is very intelligent about defining anonymous types. If the compiler sees that you are defining multiple anonymous types (3 new objects inside array) in your source code that have the identical structure, the compiler will create just one definition for the anonymous type and create multiple instances of that type.
Because of this type identity we can create an implicitly typed array of anoymous types. This works because all of the objects are of the same anonymous type.
Upvotes: 3