tagged [covariance]

covariance in c#

covariance in c# Is it possible to cast a `List` to `List` in C# 4.0? Something along these lines: Isn't this what covariance is for? if you can do: why shouldn't you be able to do than it wo

27 Oct at 22:44

Covariance with C# Generics

Covariance with C# Generics Given an interface `IQuestion` and an implementation of that interface `AMQuestion`, suppose the following example: This example yields, as expected, a compile error saying...

18 Jun at 13:30

Generic Covariance and contravariance

Generic Covariance and contravariance Consider the code snippet. But if i write `ICollection obj2 = obj;` it throws me a compile time error. > Cannot implicitly convert type '`System.Collections.Gener...

11 Jun at 01:32

C# variance problem: Assigning List<Derived> as List<Base>

C# variance problem: Assigning List as List Look at the following example (partially taken from [MSDN Blog](http://blogs.msdn.com/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part...

9 Jan at 15:59

Why classes that implement variant interfaces remain invariant?

Why classes that implement variant interfaces remain invariant? C# 4.0 has extended the co and contravariance further for generic types and interfaces. Some interfaces (like `IEnumerable`) are covaria...

28 Oct at 07:18

Why can't I use covariance with two generic type parameters?

Why can't I use covariance with two generic type parameters? Consider the following example: This compiles just fine, because `IEnumerable` is in `T`. However, if I do exactly the same thing but now w...

11 Feb at 14:46

Why can't I assign a List<Derived> to a List<Base>?

Why can't I assign a List to a List? I defined the following class: I also define a subclass of this class: There are also several other subclases of `AbstractPackageCall` Now I want to make the follo...

10 Jan at 23:56

How is Generic Covariance & Contra-variance Implemented in C# 4.0?

How is Generic Covariance & Contra-variance Implemented in C# 4.0? I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, ...

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

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

7 May at 21:29

What does <in TFrom, out TTo> mean?

What does mean? Resharper has suggested to change from into So I investigate a little and ended reading [this article](http://www.buunguyen.net/blog/new-features-of-csharp-4.html) (found through a art...

29 Nov at 20:20

What are the kinds of covariance in C#? (Or, covariance: by example)

What are the kinds of covariance in C#? (Or, covariance: by example) Covariance is (roughly) the ability to of "simple" types in complex types that use them. E.g. We can always treat an instance of `C...

Covariance and contravariance on Tasks

Covariance and contravariance on Tasks Given the followin snippet, i quite dont understand what im going to achieve is not possible: Interface: ``` public interface IEntityRepository : IRepository { ...

Casting List<T> - covariance/contravariance problem

Casting List - covariance/contravariance problem Given the following types: I wonder how can I convert a `List` to a `List`? I am not completely clear on the covariance/contravariance topics, but I un...

Difference between covariance and upcasting

Difference between covariance and upcasting What is the difference between covariance and upcasting, or, more specifically, why are they given different names? I've seen the following example referred...

15 Jul at 21:58

c# 9.0 covariant return types and interfaces

c# 9.0 covariant return types and interfaces I have two code examples: One compiles Another one does not ``` interface A { object Method1(); } class B : A { publi

2 Mar at 10:34

c# covariant return types utilizing generics

c# covariant return types utilizing generics Is the code below the only way to implement covariant return types? I want to be able to construct an Application or a NewApplication

3 Dec at 19:47

Why can't I cast from a List<MyClass> to List<object>?

Why can't I cast from a List to List? I have a List of objects, which are of my type `QuoteHeader` and I want to pass this list as a list of objects to a method which is able to accept a `List`. My li...

6 Mar at 17:16

Why covariance does not work with generic method

Why covariance does not work with generic method Assume I have interface and class: As `IEnumerable` is , the code line below is compiled successfully: But when I put it into generic method: ``` publi...

5 Oct at 15:14

Co-variant array conversion from x to y may cause run-time exception

Co-variant array conversion from x to y may cause run-time exception I have a `private readonly` list of `LinkLabel`s (`IList`). I later add `LinkLabel`s to this list and add those labels to a `FlowLa...

9 Dec at 14:4

Does I<D> re-implement I<B> if I<D> is convertible to I<B> by variance conversion?

Does I re-implement I if I is convertible to I by variance conversion? Given these type declarations, what part of the C# specification explain

Shouldn't ILookup<TKey, TElement> be (declared) covariant in TElement?

Shouldn't ILookup be (declared) covariant in TElement? The definition `System.Linq.ILookUp` reads Since `IEnumerable` is covariant in `IGrouping

23 Apr at 20:42

Why cannot IEnumerable<struct> be cast as IEnumerable<object>?

Why cannot IEnumerable be cast as IEnumerable? Why is the last line not allowed? Is this because double is a value t

13 Mar at 21:52

In C#, why can't a List<string> object be stored in a List<object> variable

In C#, why can't a List object be stored in a List variable It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way. results in Cannot implici...

Covariance and IList

Covariance and IList I would like a Covariant collection whose items can be retrieved by index. IEnumerable is the only .net collection that I'm aware of that is Covariant, but it does not have this i...

13 Apr at 19:55

Why do C# out generic type parameters violate covariance?

Why do C# out generic type parameters violate covariance? I'm unclear as to why the following code snippet isn't covarient? > Error 1 Invalid variance: The type parameter 'T' must be

18 Jan at 16:50