covariance

How is it that U[] is castable to T[]?

It is clear that the T[] array type is not covariant as the elements of a T[] can be set by index. And yet, a U[] can be cast to a T[] without any complaints from the compiler as long as U derives from T. Man[] men = new[] { new Man("Aaron"), new Man("Billy"), new Man("Charlie") }; Person[] people = (Person[])men; In the above code i...

Passing different content that implements the same interface

I have multiple Linq2Sql Classes such as "Article" "NewsItem" "Product". They all have a title, they all have a unique ID and they all have a Summary. So, I created an interface called IContent public interface IContent { int Id { get; set; } String Title { get; set; } String Summary { get; set; } String HyperLink { ge...

Interfaces covariance problem

Hi, So i have such code. public interface IGeoDataSet<out T> : IDataSet where T : IGeoPrimitive<IGeoPrimitiveContent> { IEnumerable<T> Items { get; } } public interface IDataSet { } public interface IGeoPrimitive<T> : IPrimitive where T : IGeoPrimitiveContent { T Content { get; } } public interface IPrimitive { } p...

(Co)Variance on Lists different then on Stacks in Scala?

Hi, When I'm writing this code, I've got a Compile error in Scala var s: Stack[_ <: Number] = new Stack[Integer]; s.push(new Integer(1)); //compile-error: type mismatch; found :Integer required: _$bqjyh where type _$bqjyh <: Number s.push(null); //compile-error: type mismatch; found : Null(null) required: _$2 where type _$2 <: Objec...

Build failure in unit test project with accessors of a project containing covariant types

Hello folk, Lets start at the beginning :) I added a covariant interface to our project: interface IView { } interface IPresenter<out TView> where TView : IView { TView View { get; } } I created some classes, implementing these interfaces: class TestView : IView { } class TestPresenter : IPresenter<TestView> { public TestVie...

How to write covariant readwrite properties in class continuations?

Given the following example // MyClass.h @interface MyClass { NSMutableArray *queue; } @property (readonly, retain) NSArray *queue; @end and // MyClass.m @interface MyClass () @property (readwrite, retain) NSMutableArray *queue; @end @implementation MyClass @synthesize queue; @end I get a Property 'queue' type in 'MyClas...

Entity Framework: ObjectSet and its (generics) variance

I use: EntityFramework + POCO Here is the thing: public interface IBaseType { int Id { get; set; } } public class BaseType : IBaseType { public virtual int Id { get; set; } } public class DerivedType : BaseType { } The problem: public class EntityFetcher<T> where T : BaseType { public object GetById(int id) { ...

Why .NET 4 variance for generic type arguments not also for classes?

Possible Duplicates: Why isn't there generic variance for classes in C# 4.0? Why does C# (4.0) not allow co- and contravariance in generic class types? The new .NET 4.0 co- and contravariance for generic type arguments only works for interfaces and delegates. What is the reason for not supporting it for classes too? ...

Any sensible solution to the lack of array/slice covariance in Go?

The problem I've just faced is what to do in the following case: func printItems(header string, items []interface{}, fmtString string) { // ... } func main() { var iarr = []int{1, 2, 3} var farr = []float{1.0, 2.0, 3.0} printItems("Integer array:", iarr, "") printItems("Float array:", farr, "") } Go has no generics and does...

Interfaces inheritance in C#

Hello! I'm trying to overrun quite big (for me) problem that I came across while writing my application. Look at this, please (I will try to shorten the code for simplicity): I have root interface called IRepository<T>. Next, IBookRepository : IRepository<Book> Next, concrete class that implements it: BookRepository : IBookRepository ...

C# 4.0 Generics and ASP.net MVC

It appears that in C# 4.0, variance specifiers are only applicable to interface types. So let's say I have ViewModel / EditModel classes and a simple hierarchy of models. class MyEditModel<T> where T : Base { ... } class Derived1 : Base { ... } class Derived2 : Base { ... } I have a partial view that accepts a MyEditModel of any type...

Covariance and ContraVariance with LSP

What is the relationship between LSP and Covariance and Contravariance? Is there any relationship? Is LSP a form of Covariance ? ...

Can I override an overload of an operator and return a different type?

class A{ public: virtual char &operator[](int); protected: .. }; class B:A{ public: A* &operator[](int); protected: } Can I change the return type when I overload an overload of an operator? thanks! //EDIT Okay, so now that we established that this wont work how can I build a work around? Lets say I have classe...

Generic parameter delegate?

I'm a bit fuzzy on the new Action/Func/Variance/CoVariance stuff, which is probably what I need. What I want is to be able to pass a delegate as a parameter to a method, that takes a string and returns a bool. The problem is that I can't use a typed delegate or interface since it will be used in different libraries which doesn't share l...

Question about C# covariance

In the code below: interface I1 { } class CI1: I1 { } List<CI1> listOfCI1 = new List<CI1>(); IEnumerable<I1> enumerableOfI1 = listOfCI1; //this works IList<I1> listofI1 = listOfCI1; //this does not I am able to assign my "listOfCI1" to an IEnumerable<I1> (due to covariance) But why am I not able to assign it to an IList<I1>? For t...

covariance in c#

Is it possible to cast a List<Subclass> to List<Superclass> in C# 4.0? Something along these lines: class joe : human {} List<joe> joes = GetJoes(); List<human> humanJoes = joes; Isn't this what covariance is for? if you can do: human h = joe1 as human; why shouldn't you be able to do List<human> humans = joes as List<human>...