covariance

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? ...

How is generic function implemented in java?

Hi, As per my understanding the following generic function in java: public static <T> T f(T x) { Integer[] arr = new Integer[4]; T ret = (T) arr[2]; return ret; } is compiled to the following form (as it is unbounded): public static Object f(Object x) { Integer[] arr = new Integer[4]; Object ret = (Object) arr[2]; r...

C# Covariance issue

I had a linq-to-sql generated domain entity that I cast to the proper interface like so: public IEnumerable<IApplication> GetApplications() { using (var dc = new LqDev202DataContext()) { return dc.ZApplications.Cast<IApplication>().ToList(); } } However I renamed the linq-to-sql table withou...

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...

Co- and Contravariance bugs in .NET 4.0

Some strange behavior with the C# 4.0 co- and contravariance support: using System; class Program { static void Foo(object x) { } static void Main() { Action<string> action = _ => { }; // C# 3.5 supports static co- and contravariant method groups // conversions to delegates types, so this is perfectly legal: action...

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...

Delegate Covariance Confusion Conundrum!

Why does this not work? Do I not understand delegate covariance correctly? public delegate void MyDelegate(object obj) public class MyClass { public MyClass() { //Error: Expected method with 'void MyDelegate(object)' signature _delegate = MyMethod; } private MyDelegate _delegate; public void MyMe...

Any simple way to explain why I cannot do List<Animal> animals = new ArrayList<Dog>()?

I know why one shouldn't do that. But is there way to explain to a layman why this is not possible. You can explain this to a layman easily : Animal animal = new Dog();. A dog is a kind of animal but a list of dogs is not a list of animals. ...

Contravariance and Entity Framework 4.0: how to specify EntityCollection as IEnumerable?

I have specified a couple of interfaces, which I am implementing as entities using Entity Framework 4. The simplest demonstration code I can come up with is: public class ConcreteContainer : IContainer { public EntityCollection<ConcreteChild> Children { get; set; } } public class ConcreteChild : IChild { } public interfa...

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...

C++: How can I avoid "invalid covariant return type" in inherited classes without casting?

I have a quite complex class hierarchy in which the classes are cross-like depending on each other: There are two abstract classes A and C containing a method that returns an instance of C and A, respectively. In their inherited classes I want to use a co-variant type, which is in this case a problem since I don't know a way to forward-d...

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 ...

C# 4.0 RC, Silverlight 4.0 RC Covariance

Hi, I am trying to develop a Silverlight 4 application using C# 4.0. I have a case like this: public class Foo<T> : IEnumerable<T> { .... } Elsewhere: public class MyBaseType : MyInterface { ... } And the usage where I am having problems: Foo<MyBaseType> aBunchOfStuff = new Foo<MyBaseType>(); Foo<MyInterface> moreGeneralS...

Is there a way to achieve covariance of generic types in C# 3.0?

This has been introduced in C# 4.0, but is there a way to achieve this in c# 3.0? For e.g., consider the following code: class Base { } class Derived1 : Base { } class Derived2 : Base { } class User<T> where T : Base { } class User1 : User<Derived1> { } Now, I would like to have a list of User<T>, in which I can store User<Derive...

How to specify generic method type parameters partly

I have an extension method like below: public static T GetValueAs<T, R>(this IDictionary<string, R> dictionary, string fieldName) where T : R { R value; if (!dictionary.TryGetValue(fieldName, out value)) return default(T); return (T)value; } Currently, I can use it in the following way: var dictionary = n...

C# cast Foo<Bar> to Foo<object>

Hi, does anyone know if it is possible to cast a generic type with a certain type parameter (e.g. Bar) to the same generic type with the type parameter being a base type of Bar (such as object in my case). And, if it is possible, how would it be done? What I want to do is have a collection of Foo<object> but be able to add Foos with mor...

java generics covariance

I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant the author states, Because ln is a List, adding a Float to it seems perfectly legal. But if ln were aliased with li, then it would break the type-safety promise impli...

Casting generics and the generic type

Consider, I have the following 3 classes / interfaces: class MyClass<T> { } interface IMyInterface { } class Derived : IMyInterface { } And I want to be able to cast a MyClass<Derived> into a MyClass<IMyInterface> or visa-versa: MyClass<Derived> a = new MyClass<Derived>(); MyClass<IMyInterface> b = (MyClass<IMyInterface>)a; But I...

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 ...

How to accomplish covariant return types when returning a shared_ptr?

using namespace boost; class A {}; class B : public A {}; class X { virtual shared_ptr<A> foo(); }; class Y : public X { virtual shared_ptr<B> foo(); }; The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around thi...