covariance

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

眉间皱痕 提交于 2019-11-28 16:53:42
问题 I have a private readonly list of LinkLabel s ( IList<LinkLabel> ). I later add LinkLabel s to this list and add those labels to a FlowLayoutPanel like follows: foreach(var s in strings) { _list.Add(new LinkLabel{Text=s}); } flPanel.Controls.AddRange(_list.ToArray()); Resharper shows me a warning: Co-variant array conversion from LinkLabel[] to Control[] can cause run-time exception on write operation . Please help me to figure out: What does this means? This is a user control and will not be

Generic type parameter covariance and multiple interface implementations

半世苍凉 提交于 2019-11-28 16:39:34
If I have a generic interface with a covariant type parameter, like this: interface IGeneric<out T> { string GetName(); } And If I define this class hierarchy: class Base {} class Derived1 : Base{} class Derived2 : Base{} Then I can implement the interface twice on a single class, like this, using explicit interface implementation: class DoubleDown: IGeneric<Derived1>, IGeneric<Derived2> { string IGeneric<Derived1>.GetName() { return "Derived1"; } string IGeneric<Derived2>.GetName() { return "Derived2"; } } If I use the (non-generic) DoubleDown class and cast it to IGeneric<Derived1> or

Simple examples of co and contravariance

纵然是瞬间 提交于 2019-11-28 15:58:56
Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). All samples I've seen so far was just casting some object into System.Object . Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). I have no idea what "contra-invariance" means. The rest are easy. Here's an example of covariance: void FeedTheAnimals(IEnumerable<Animal> animals) { foreach(Animal animal in animals) animal.Feed(); } ... List<Giraffe> giraffes = ...; FeedTheAnimals

How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues

社会主义新天地 提交于 2019-11-28 13:22:16
I am trying to combine (union or concat) two lists/collection into one. The two lists have a common base class. e.g. I've tried this: IQueryable<ContractItem> contractItems = myRepository.RetrieveContractItems(); IQueryable<ChangeOrderItem> changeOrderItems = myRepository.RetrieveChangeOrderItems(); IQueryable<ItemBase> folderItems = contractItems.Concat<ItemBase>(changeOrderItems); But am getting the LINQ error DbUnionAllExpression requires arguments with compatible collection ResultTypes. Anybody know how to do this properly? The only thing I could google was another StackOverflow question:

Generic Class Covariance

放肆的年华 提交于 2019-11-28 12:39:06
Is it possible to make the following code compile in C#? I do compile similar in Java. public interface IInterface { ... } public class Class1 : IInterface { ... } public abstract class Base<T> where T : IInterface { ... } public class Class2<T> : Base<T> where T : IInterface { ... } . . . public SomeMethod() { List<Base<IInterface>> list = new List<Base<IInterface>>(); Class2<Class1> item = new Class2<Class1>(); list.Add(item); // Compile error here } No, that is not legal in C#. C# 4 and above support covariance and contravariance of generic interfaces and generic delegates when they are

Generic constraint ignores co-variance

早过忘川 提交于 2019-11-28 12:17:45
Let's say we have an interface like public interface IEnumerable<out T> { /*...*/ } that is co-variant in T . Then we have another interface and a class implementing it: public interface ISomeInterface {} public class SomeClass : ISomeInterface {} Now the co-variance allows us to do the following IEnumerable<ISomeInterface> e = Enumerable.Empty<SomeClass>(); So a IEnumerable<SomeClass> is assignable to a variable (or method parameter) of type IEnumerable<ISomeInterface> . But if we try this in a generic method: public void GenericMethod<T>(IEnumerable<T> p) where T : ISomeInterface {

Covariant return type and type conversion

亡梦爱人 提交于 2019-11-28 12:12:06
s->duplicate() returns an object of type Box* , but I'm getting an error initializing it with Box* . It looks like it's being converted back to Shape* . What is the point of having covariant return types if it's converted back to the base class pointer?: struct Shape { virtual Shape* duplicate() { return new Shape; } }; struct Box : Shape { virtual Box* duplicate() { return new Box; } }; int main() { Shape* s = new Box; Box* b = s->duplicate(); } Error: main.cpp:22:12: error: cannot initialize a variable of type 'Box *' with an rvalue of type 'Shape *' Box* b = s->duplicate(); ^ ~~~~~~~~~~~~~~

Why can't I assign List<int> to IEnumerable<object> in .NET 4.0

萝らか妹 提交于 2019-11-28 12:03:36
I try to do this: IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" }; it works great! But when I try to do this: IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 }; Visual Studio tells me: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) Why is that? int is a value type and can only be boxed to object - it doesn't inherit from object . Since you're using an IEnumerable<object> anyway, this should work: IEnumerable intIds = new List<int>() { 1,

When is C++ covariance the best solution?

↘锁芯ラ 提交于 2019-11-28 09:38:17
This question was asked here a few hours ago and made me realise that I have never actually used covariant return types in my own code. For those not sure what covariance is, it's allowing the return type of (typically) virtual functions to differ provided the types are part of the same inheritance hierarchy. For example: struct A { virtual ~A(); virtual A * f(); ... }; struct B : public A { virtual B * f(); ... }; The different return types of the two f() functions are said to be covariant. Older versions of C++ required the return types to be the same, so B would have to look like: struct B

How to make generic class that contains a Set of only its own type or subtypes as Children?

我的未来我决定 提交于 2019-11-28 08:26:28
问题 abstract class Animal { } class Mammal : Animal { } class Dog : Mammal { } class Reptile : Animal { } class AnimalWrapper<T> where T : Animal { public ISet<AnimalWrapper<T>> Children { get; set; } } class Program { public static void Main(string[] args) { var foo = new AnimalWrapper<Mammal>(); foo.Children = new HashSet<AnimalWrapper<Mammal>>(); var child = new AnimalWrapper<Dog>(); foo.Children.Add(child); } } This obviously doesn't compile because of foo.Children.Add(child); I'm not sure if