The last meeting has been quite a long time ago because one of us three took a vacation of a couple of weeks. So here we are back again.
The topic of this meeting is chapter 3: Parameterized typing with generics.
As I am now in my 4th year of developing with C# 2 there have been few really new and stunning facts in this chapter. Still I’ve learned quit a good bit and I was able to pick up quite a couple of terms that help me clarify my understanding of how generics work.
Here are the main points that were very valuable to me:
- The diagram on page 68 is really an eye opener as it explains the relationship between unbound generic types, constructed (generic) types and instances. I wont put a copy of this diagram here. Go get the book!
- The way the runtime constructs a type from an unbound generic type for each different combination of the generic parameters.
- The way comparison are performed with the == and != operators based on the constraints on the generic type parameters as explained on pp 82, 83.
- Very interesting is the demonstration on page 87 that each constructed type has its own static constructor .
- I cannot remember finding any hint, that there are no generic methods in C++. Which is obvious if you look at how templates are a king of "preprocessor on steroids". The generic parameters of generic methods are only available at runtime (are they really?) but not at compile time.
Which leads me to another question: Are generic parameters of methods resolved based on the runtime or on the compile time type?
- OMG: Covariance and Contravariance again!
- On p. 103 the options for working arround the invariance of generic types. The last item talks about a generic helper method to convert the items in a collection. Here should be mentioned that there is a method System.Linq.Enumerable.Cast
(IEnumberable) that does just that. I think a hint would be in order even if this C# 3 features are dealt with later in the book.