Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C# 4.0's covariance/contravariance limited to parameterized interface and delegate types?

Is this a limitation of the CLR or are there compatibility concerns with existing code?

Is this related to the messed up variance of delegate combining in C# 4.0?

Edit: Would it be possible to have a language using co-/contravariance without that limitation running on the CLR?

like image 488
soc Avatar asked Aug 04 '10 13:08

soc


People also ask

Why is it named C?

C is a general purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. It was named 'C' because many of its features were derived from an earlier language called 'B'.

Why do we use C?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

What is called C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

You are going to want to read Eric Lippert's post on why it works the way it does. The short of it is that they allowed as much variance as possible without allowing developers to make bad mistakes in programming that could cause difficult to track down errors. The amount of variance in 4.0 is greatly expanded over the 3.0 rules, and from what I understand it was a balance between what is benificial to the developer and what is safe to allow without causing too much of a headache through unintentional mistakes.

http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/default.aspx

like image 101
kemiller2002 Avatar answered Sep 30 '22 16:09

kemiller2002