Tag: Covariance and Contravariance

I missed the party. I was all set to be on that massive wave of announcements about TypeScript, and then a family emergency kept me away from computers from Thursday of last week until just now, and I did not get my article in the queue. Suffice to say that I am SUPER EXCITED about…

Happy New Year all! It has just been brought to my attention that this blog and the Programmer Ryan Gosling photo blog share at least one reader: I admit it, I LOL’d. In the interests of total accuracy I’d like to point out that the first entry on the blog contains a subtle error: .NET…

Here’s a pattern you see all the time in C#: class Frob : IComparable<Frob> At first glance you might ask yourself why this is not a “circular” definition; after all, you’re not allowed to say “class Frob : Frob”(*). However, upon deeper reflection that makes perfect sense; a Frob is something that can be compared…

I thought it might be interesting for you all to get a precise description of how exactly it is that we determine when it is legal to put “in” and “out” on a type parameter declaration in C# 4. I’m doing this here because (1) it’s of general interest, and (2) our attempt to make…

I’ve written a lot about this already, but I think one particular point bears repeating. As we’re getting closer to shipping C# 4.0, I’m seeing a lot of documents, blogs, and so on, attempting to explain what “covariant” means. This is a tricky word to define in a way that is actually meaningful to people…

Somehow it has happened again; people just keep on recording videos of me and putting them on the internet. In these videos you find out what I look like when lit from above and behind. Kinda spooky. We should have made the room entirely dark and held a flashlight underneath my face. That would be,…

Here’s a good question from StackOverflow: If you have a method that takes an “X” then you have to pass an expression of type X or something convertible to X. Say, an expression of a type derived from X. But if you have a method that takes a “ref X”, you have to pass a…

[UPDATES below] A while back I described a kind of variance that we’ve supported since C# 2.0. When assigning a method group to a delegate type, such that both the selected method and the delegate target agree that their return type is a reference type, then the conversion is allowed to be covariant. That is,…

(Note: not to be confused with Inheritance and Representation.) I get a fair number of questions about the C# cast operator. The most frequent question I get is: short sss = 123;object ooo = sss; // Box the short.int iii = (int) sss; // Perfectly legal.int jjj = (int) (short) ooo; // Perfectly legalint kkk…