What is the real reason for this limitation? Is this just a job to do? Is it conceptually difficult? It's impossible?
Of course, you cannot use type parameters in fields, because they always read and write. But that can't be the answer, can it?
The reason for this question is because I am writing an article on dispersion support in C # 4, and I feel that I have to explain why it is limited to delegates and interfaces. Just to pay attention to the burden of proof.
Update: Eric asked about an example.
How about this (I don't know if that makes sense, but :-))
public class Lookup<out T> where T : Animal { public T Find(string name) { Animal a = _cache.FindAnimalByName(name); return a as T; } } var findReptiles = new Lookup<Reptile>(); Lookup<Animal> findAnimals = findReptiles;
The reason for this in one class may be the cache that is stored in the class itself. And please do not call your animals of different types the same!
By the way, this leads me to optional type parameters in C # 5.0 :-)
Update 2: I am not saying that the CLR and C # should allow this. Just trying to understand what led to the fact that this is not so.
Lars Corneliussen Mar 29 '10 at 21:40 2010-03-29 21:40
source share