Sign In
Fabulous Adventures In Coding
Eric Lippert's Blog
Tags
Aargh! (8)
accuracy (6)
anonymous types (3)
Arrays (8)
ASP (11)
AStar (5)
Async (15)
bad jokes (14)
Begging the question (4)
Benford's Law (3)
Best Of FAIC (12)
Big Words (5)
Books (22)
Breaking Changes (24)
Brittle Base Classes (6)
C# (317)
C# 4.0 (39)
C# 5.0 (9)
Cargo Cult Programming (4)
cast operator (3)
Channel 9 (6)
Charts (4)
closures (3)
Code Generation (10)
Code Quality (29)
COM Programming (57)
Conditional Operator (3)
Continuation Passing Style (11)
Conversions (16)
Covariance and Contravariance (22)
customer service (4)
declaration spaces (5)
Dialogue (14)
English Usage (11)
exception handling (9)
Floating Point Arithmetic (14)
grammars (9)
graph colouring (5)
Hashing (8)
High Dimensional Spaces (5)
Immutability (27)
integer arithmetic (5)
Interviewing (8)
Introduction (6)
It Hurts When I Do This (5)
Iterators (10)
JScript (93)
JScript .NET (29)
keywords (4)
Lambda Expressions (20)
Language Design (59)
local variables (3)
localization (3)
Mathematics (14)
Memory Management (13)
Metablogging (9)
Mistakes (4)
Music (6)
myths (7)
namespaces (5)
Non-computer (37)
Optional arguments (5)
Overload Resolution (9)
Pages (25)
Performance (48)
precedence (4)
precision (7)
protected (7)
Puzzles (46)
queries (3)
quotable quotations (4)
Quotes (3)
Rants (51)
Rarefied Heights (51)
Recursion (26)
reference (4)
Regular Expressions (13)
Relationships (4)
Salt (4)
Science (12)
scope (5)
Scripting (189)
Security (46)
shadowcasting (6)
SimpleScript (30)
Software development methodology (13)
Static Methods (6)
Threading (18)
Topological Sort (4)
Type Inference (17)
type safety (4)
unsafe code (4)
Value Types (11)
VBScript (80)
Video (12)
virtual dispatch (9)
VSTO (10)
warnings (5)
What's The Difference? (11)
Zombies (4)
Browse by Tags
MSDN Blogs
>
Fabulous Adventures In Coding
>
All Tags
>
covariance and contravariance
Tagged Content List
Blog Post:
He's So Dreamy
Eric Lippert
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 actually supported...
on
2 Jan 2012
Blog Post:
Curiouser and curiouser
Eric Lippert
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...
on
3 Feb 2011
Blog Post:
Exact rules for variance validity
Eric Lippert
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 a more human-readable...
on
3 Dec 2009
Blog Post:
What's the difference between covariance and assignment compatibility?
Eric Lippert
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...
on
30 Nov 2009
Blog Post:
Some new videos
Eric Lippert
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...
on
26 Oct 2009
Blog Post:
Why is covariance of value-typed arrays inconsistent?
Eric Lippert
Another interesting question from StackOverflow : uint[] foo = new uint[10]; object bar = foo; Console.WriteLine("{0} {1} {2} {3}", foo is uint[], // True foo is int[], // False bar is uint[], // True bar is int[]); // True What the heck is going on here? This program fragment illustrates an interesting...
on
24 Sep 2009
Blog Post:
Why do ref and out parameters not allow type variation?
Eric Lippert
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 ref to a variable of type...
on
21 Sep 2009
Blog Post:
The void is invariant
Eric Lippert
[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...
on
29 Jun 2009
Blog Post:
Representation and Identity
Eric Lippert
(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...
on
19 Mar 2009
Blog Post:
The Future of C#, Part Four: Yet More Video
Eric Lippert
Mads, Chris, Sam and me on Channel Nine. Enjoy.
on
2 Nov 2008
Blog Post:
Covariance and Contravariance, Part Eleven: To infinity, but not beyond
Eric Lippert
UPDATE: Andrew Kennedy, author of the paper I reference below, was good enough to point out some corrections and omissions, which I have addressed. Thanks Andrew! As I've discussed at length in this space, we are considering adding covariance and contravariance on delegate and interface types parameterized...
on
7 May 2008
Blog Post:
Immutability in C# Part Three: A Covariant Immutable Stack
Eric Lippert
Now suppose we had a hypothetical future version of C# in which interface covariance worked, and we wanted a covariant immutable stack. That is, we want to be able to implicitly convert an IStack<Giraffe> to IStack<Mammal> . As we've already discussed, this doesn't make much sense in an array...
on
6 Dec 2007
Blog Post:
Covariance and Contravariance in C#, Part Ten: Dealing With Ambiguity
Eric Lippert
OK, I wasn’t quite done. One more variance post! Smart people: suppose we made IEnumerable<T> covariant in T . What should this program fragment do? class C : IEnumerable<Giraffe>, IEnumerable<Turtle> { IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator() { yield...
on
9 Nov 2007
Blog Post:
Covariance and Contravariance in C#, Part Nine: Breaking Changes
Eric Lippert
Today in the last entry in my ongoing saga of covariance and contravariance I’ll discuss what breaking changes adding this feature might cause. Simply adding variance awareness to the conversion rules should never cause any breaking change. However, the combination of adding variance to the conversion...
on
2 Nov 2007
Blog Post:
Covariance and Contravariance in C#, Part Eight: Syntax Options
Eric Lippert
As I discussed last time, were we to introduce interface and delegate variance in a hypothetical future version of C# we would need a syntax for it. Here are some possibilities that immediately come to mind. Option 1: interface IFoo<+T, -U> { T Foo(U u); } The CLR uses the convention I...
on
31 Oct 2007
Blog Post:
Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?
Eric Lippert
Suppose we were to implement generic interface and delegate variance in a hypothetical future version of C#. What, hypothetically, would the syntax look like? There are a bunch of options that we could hypothetically consider. Before I get into options though, let’s be bold. What about “no syntax...
on
29 Oct 2007
Blog Post:
Covariance and Contravariance in C#, Part Six: Interface Variance
Eric Lippert
Over the last few posts I’ve discussed how it is possible to treat a delegate as contravariant in its arguments and covariant in its return type. A delegate is basically just an object which represents a single function call; we can do this same kind of thing to other things which represent function...
on
26 Oct 2007
Blog Post:
Covariance and Contravariance In C#, Part Five: Higher Order Functions Hurt My Brain
Eric Lippert
Last time I discussed how we could in a hypothetical future version of C# allow delegate types to be covariant in their return type and contravariant in their formal parameter types. For example, we could have a contravariant action delegate: delegate void Action< -A > (A a); and then have...
on
24 Oct 2007
Blog Post:
Covariance and Contravariance in C#, Part Four: Real Delegate Variance
Eric Lippert
In the last two posts I discussed the two kinds of variance that C# already has -- array covariance and member-group-to-delegate conversion covariance (on return types) and contravariance (on formal parameter types). Today I want to generalize the latter kind of variance. In C# 3.0 today, even...
on
22 Oct 2007
Blog Post:
Covariance and Contravariance in C#, Part Three: Method Group Conversion Variance
Eric Lippert
Last time I discussed how array covariance is broken in C# (and Java, and a number of other languages as well.) Today, a non-broken kind of variance supported by C# 2.0: conversions from method groups to delegates. This is a more complicated kind of variance, so let me spell it out in more detail. ...
on
19 Oct 2007
Blog Post:
Covariance and Contravariance in C#, Part Two: Array Covariance
Eric Lippert
C# implements variance in two ways. Today, the broken way. Ever since C# 1.0, arrays where the element type is a reference type are covariant . This is perfectly legal: Animal[] animals = new Giraffe[10]; Since Giraffe is smaller than Animal , and “make an array of” is a covariant operation...
on
17 Oct 2007
Blog Post:
Covariance and Contravariance in C#, Part One
Eric Lippert
I have been wanting for a long time to do a series of articles about covariance and contravariance (which I will shorten to “variance” for the rest of this series.) I’ll start by defining some terms, then describe what variance features C# 2.0 and 3.0 already support today, and then discuss some ideas...
on
16 Oct 2007
Page 1 of 1 (22 items)