问题
weird problem...
Ive implemented covariance from example .
My target FW is 4.0.
How ever . I wanted to see if it fails on 3.5 /2 -> but it wont.
It cant be since covariance is from FW4.0.
After changing to 3.0 i Build and see : (+wont fail on runtime)
回答1:
Covariance was supported in framework 3, but not implemented in the compiler. You are using compiler 4 targeting framework 3.
This is mentioned in an Eric Lippert blog post, I'll see if I can find it..
EDIT
To further clarify,: The Common Language Runtime and the C# compiler (csc.exe) are two separate things. Covariance and Contravariance where introduced to the CLR at version 2.0. They were not introduced to C# until version 4. You are using Compiler 4 to build version 3 IL which is why it works.
回答2:
The CLR has supported covariant and contravariant generic type parameters since version 2.0. It is just the language C# that didn't provide any syntax for it until version 4.
来源:https://stackoverflow.com/questions/9210483/covariance-also-in-3-5-2-0