covariance

Why is conversion between with different type parameters is NOT allowed?

I just don't get it as it would be so useful to convert one generic container into the other? Stack <IType> stack = new Stack<SomeType>(); ...

What are the benefits of covariance and contravariance?

C# 4.0 is going to support covariance and contravariance. But I don't clearly understand the benefits of this new feature. Can you explain me (clearly) why we need it? ...

Why does C#/CLR not support method override co/contra-variance?

There are quite a few questions & answers about hacking around the limitation of C# not allowing method return (and argument) types to be changed to compatible types on overrides, but why does this limitation exist, either in the C# compiler or in the CLR? As I an see, there is nothing that could break if co/contra-variance was allowed, ...

How to make a generic class with inheritance?

How can I make the following code work? I don't think I quite understand C# generics. Perhaps, someone can point me in the right direction. public abstract class A { } public class B : A { } public class C : A { } public static List<C> GetCList() { return new List<C>(); } ...

Type variance in .NET Framework 4.0

IEnumerable<T>, IComparable<T> and a few more are now type-variant. IList<T>, ICollection<T> and many others aren't. Why? ...

Non Generic Versions of Generic classes and interfaces.

I often find myself in a situation where I create a generic interface or class and then want to use different versions of this class or interface in a non generic way. For example I may have an interface like this: interface ICanCreate<T> { T NewObject(); } Which allows a class to be a factory for that type. I then want to registe...

C#: Overriding return types

Is there way to override return types in C#? If so how, and if not why and what is a recommended way of doing it? My case is that I have an interface with an abstract base class and descendants of that. I would like to do this (ok not really, but as an example!) : public interface Animal { Poo Excrement { get; } } public class Anim...

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism ?

I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it's only another word for polymorphism. Am I correct with the above statement? Or have I got it wrong ? ...

Covariant virtual functions and smart pointers

In C++, a subclass can specify a different return type when overriding a virtual function, as long as the return type is a subclass of the original return type (And both are returned as pointers/references). Is it possible to expand this feature to smart pointers as well? (Assuming a smart pointer is some template class) To illustrate:...

Covariance and contravariance in programming languages

Can anyone explain me, the concept of covariance and contravariance in programming languages theory? ...

Inheritance in C# - simple question

Duplicate In C#, why can’t a List object be stored in a List variable Here is my code: public class Base { protected BindingList<SampleBase> m_samples; public Base() { } } public class Derived : Base { public Derived() { m_samples = new BindingList<SampleDerived>();...

Solution to the lack of covariance with generics in c# 2.0 (BindingList)

It's a design question. I have a business object, and 5 business object types that are derived from it. I also will have a class which has BindingList as a member. I will have 5 classes derived from it. Since covariance doesn't work here, how would you structure the design to minimize code repetition? I could of course chuck the Bind...

Java covariance question

I'm having a hard time trying to figure this out. Say I have the following code: class Animal { } class Mammal extends Animal { } class Giraffe extends Mammal { } ... public static List<? extends Mammal> getMammals() { return ...; } ... public static void main(String[] args) { List<Mammal> mammals = getMammals(); // compilation err...

How can i cast into a ObservableCollection<object>

How can i cast from ObservableCollection<TabItem> into ObservableCollection<object> this doesnt work for me (ObservableCollection<object>)myTabItemObservableCollection ...

C# Type Conversion Error Despite Generic Constraint

Why, with a generic constraint on type parameter T of class P of "must inherit from A", does the first call succeed but the second call fail with the type conversion error detailed in the comment: abstract class A { } static class S { public static void DoFirst(A argument) { } public static void DoSecond(ICollection<A> argument...

Why is List<Number> not a sub-type of List<Object>?

public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not type-check. I can't even cast the parameter as follows: wahey((List<Object>) new LinkedList<Number>()); From my research, I have gathered that the reason for not allowing this is type-safety. If we were allowed to do the above, ...

Co-/contravariance support for non-generic types?

I wonder why the C# team decided not to support co-/contravariance for non-generics, considering they could be just as safe. The question is rather subjective, as I don't expect a team member to respond, but someone may have the insight I (and Barbara Liskov) lack. Lets look at this sample interface: public interface ITest { object Pr...

IList using covariance and contravariance in c#, is this possible?

Hi all would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could us...

Is there a way to forward declare covariance?

Suppose I have these abstract classes Foo and Bar: class Foo; class Bar; class Foo { public: virtual Bar* bar() = 0; }; class Bar { public: virtual Foo* foo() = 0; }; Suppose further that I have the derived class ConcreteFoo and ConcreteBar. I want to covariantly refine the return type of the foo() and bar() methods like this: ...

Problem with Covariant return types from an abstract method

I’m trying to wrap up a two day beat down on Abstract methods and return type Covariance, I’ve already posted two similar questions and I am eternally grateful to the community for the info provided, I just need one last push to get to the finish line. Here is what I am trying to do: 2 abstract classes, RecruiterBase and CandidateBase, b...