contravariance

Contravariance explained

First of, I have read many explanations on SO and blogs about covariance and contravariance and a big thanks goes out to Eric Lippert for producing such a great series on Covariance and Contravariance. However I have a more specific question that I am trying to get my head around a little bit. As far as I understand per Eric's explanat...

Difference between Covariance & Contra-variance

I am having trouble understanding the difference between covariance and contravariance. My understanding is one supports out, as in "action" with no return type, which is casting to. The other takes in an argument and is casting from, is this correct? ...

Generic Variance in C# 4.0

Generic Variance in C# 4.0 has been implemented in such a way that it's possible to write the following without an exception (which is what would happen in C# 3.0): List<int> intList = new List<int>(); List<object> objectList = intList; [Example non-functional: See Jon Skeet's answer] I recently attended a conference where Jon Ske...

Covariance, Contravariance and Delegate Problem

Hi there, I again need help by you, this time I struggle with covariance, contravariance, delegates and an simple idea blowing up... I want to implement an attribute for our businessobject-properties that takes a delegate and the needed parameters for that one, so that I can work with reflection, read out the attribute and perform a va...

Instantiating a list of parameterized types, making beter use of Generics and Linq

I'm hashing a file with one or more hash algorithms. When I tried to parametrize which hash types I want, it got a lot messier than I was hoping. I think I'm missing a chance to make better use of generics or LINQ. I also don't like that I have to use a Type[] as the parameter instead of limiting it to a more specific set of type (H...

Why does C# (4.0) not allow co- and contravariance in generic class types?

What is the real reason for that limitation? Is it just work that had to be done? Is it conceptually hard? Is it impossible? Sure, one couldn't use the type parameters in fields, because they are allways read-write. But that can't be the answer, can it? The reason for this question is that I'm writing an article on variance support in ...

Does C# 4's covariance support nesting of generics?

I don't understand why 'x' below converts, but 'y' and 'z' do not. var list = new List<List<int>>(); IEnumerable<List<int>> x = list; List<IEnumerable<int>> y = list; IEnumerable<IEnumerable<int>> z = list; Does the new covariance feature simply not work on generics of generics or am I doing something wrong? (I'd like to avoid using ...

Can't contravariance be solved with interfaces?

Hi, I'm at the point where I'm starting to grasp contravariance, although I'm trying to work out what the advantage is when an interface can be used instead. Obviously I'm missing something. Here is the c#4 example class Dog : Animal { public Dog(string name) : base(name) { } } class Animal { string _name; ...

Understanding Covariant and Contravariant interfaces in C#

I've come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context. Is there a good concise explanation of what they are and what they are useful for out there? Edit for clarification: Covariant interface: interface IBibble<out T> . . Contravariant interface: int...

Covariance and Contravariance inference in C# 4.0

When we define our interfaces in C# 4.0, we are allowed to mark each of the generic parameters as in or out. If we try to set a generic parameter as out and that'd lead to a problem, the compiler raises an error, not allowing us to do that. Question: If the compiler has ways of inferring what are valid uses for both covariance (out) an...

.NET 4.0 Generic Invariant, Covariant, Contravariant

Here's the scenario i am faced with: public abstract class Record { } public abstract class TableRecord : Record { } public abstract class LookupTableRecord : TableRecord { } public sealed class UserRecord : LookupTableRecord { } public interface IDataAccessLayer<TRecord> where TRecord : Record { } public interface ITable...

Can I have a type that's both, covariant and contravariant, i.e. fully fungible/changeable with sub and super types?

Just a stupid question. I could try it out in 2 minutes, really. It's just that I have 1 GB RAM and have already got 2 instances of VS 2010 open on my desktop, with an instance of VS 2005, too. Opening another instance of VS 2010 would be an over kill. Can I have a type (for now forgetting its semantics) that can be covariant as well as...

In C#, are event handler arguments contravariant?

If I have a class that raises an event, with (e.g.) FrobbingEventArgs, am I allowed to handle it with a method that takes EventArgs? Here's some code: class Program { static void Main(string[] args) { Frobber frobber = new Frobber(); frobber.Frobbing += FrobberOnFrobbing; frobber.Frob(); } private static ...

Detect variance on generic type parameters of interfaces

Is there a way to reflect on an interface to detect variance on its generic type parameters and return types? In other words, can I use reflection to differentiate between the two interfaces: interface IVariant<out R, in A> { R DoSomething(A arg); } interface IInvariant<R, A> { R DoSomething(A arg); } The IL for both looks the...

ref and out parameters in C# and cannot be marked as variant.

What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the followi...

Why can I not convert from List<IScreen> to List<IRenderable> even when IScreen implements IRenderable?

Pretty self explanatory question, glad for any answer. For me it doesn't make sense. I don't care about losing information. I just want to concatenate all my lists that inherit IRenderable somehow into a single list and then call IRenderable methods on it. I have a feeling that this is some basic rule of programming I forgot... edit: T...

Is there a way to determine the Variance of an Interface / Delegate in C# 4.0?

So now that we have generic Covariance and Contravariance on interfaces and delegates in C#, I was just curious if given a Type, you can figure out the covariance/contravariance of its generic arguments. I started trying to write my own implementation, which would look through all of the methods on a given type and see if the return type...

Why aren't classes in .NET 4 covariant?

Possible Duplicate: Why isnt there generic variance for classes in C# 4.0? As a rookie programmer I have a couple of questions about variance in .NET 4. Not so much about how it works, but why certain things are not variant and if other people would find this useful. Question 1: I know that interfaces and delegates can be c...

Contravariant Delegates Value Types

Can anyone shed light on why contravariance does not work with C# value types? The below does not work private delegate Asset AssetDelegate(int m); internal string DoMe() { AssetDelegate aw = new AssetDelegate(DelegateMethod); aw(32); return "Class1"; } private static House DelegateMethod(object m) { return null; } ...

could someone explain the connection between type covariance/contravariance and category theory?

I am just starting to read about category theory, and would very much appreciate it if someone could explain the connection between CS contravariance/covariance and category theory. What would some example categories be (i.e. what are their objects/morphisms?)? Thanks in advance? ...