contravariance

C# delegate contravariance with lambda expression [duplicate]

99封情书 提交于 2019-11-28 08:56:50
问题 This question already has an answer here: Can’t assign to delegate an anonymous method with less specific parameter type 3 answers The second test method below does not compile (cannot convert lambda expression to target type D1 ). Does that mean that (non-generic) delegate contravariance does not work with lambda expressions? [TestFixture] public class MyVarianceTests { private abstract class Animal {} private class Tiger : Animal {} private delegate Type D1(Tiger tiger); private static Type

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

Why covariance does not work with generic method

旧巷老猫 提交于 2019-11-28 08:10:14
Assume I have interface and class: public interface ITree {} public class Tree : ITree {} As IEnumerable<T> is covariant , the code line below is compiled successfully: IEnumerable<ITree> trees = new List<Tree>(); But when I put it into generic method: public void Do<T>() where T : ITree { IEnumerable<ITree> trees = new List<T>(); } I get compiled error from compiler: Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) D:\lab\Lab.General\Lab.General\Program.cs 83 40 Lab

How to make a generic class with inheritance?

只谈情不闲聊 提交于 2019-11-28 06:22:32
问题 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>(); } static void Main(string[] args) { List<A> listA = new List<A>(); listA.Add(new B()); listA.Add(new C()); // Compiler cannot implicitly convert List<A> listB = new List<B>(); // Compiler cannot implicitly convert List<A>

Generic Variance in C# 4.0

痞子三分冷 提交于 2019-11-28 05:59:03
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 Skeet gave an excellent overview of Generic Variance, but I'm not sure I'm completely getting it - I understand the significance of the in and out key words when it comes to contra and co-variance, but I'm curious to what happens behind the scenes. What does the CLR see

How to check covariant and contravariant position of an element in the function?

北战南征 提交于 2019-11-27 23:14:54
This is a code snippet from one of the articles that I read regarding contravariance and covariance in scala. However, I fail to understand the error message thrown by the scala compiler "error: covariant type A occurs in contravariant position in type A of value pet2 class Pets[+A](val pet:A) { def add(pet2: A): String = "done" } My understanding of this code snippet is that Pets is covariant and accepts objects that are subtypes of A. However, the function add takes in a parameter of type A only.Being covariant means that the Pets can take parameters of Type A and its subtypes. Then how is

How to check covariant and contravariant position of an element in the function?

回眸只為那壹抹淺笑 提交于 2019-11-27 19:10:52
问题 This is a code snippet from one of the articles that I read regarding contravariance and covariance in scala. However, I fail to understand the error message thrown by the scala compiler "error: covariant type A occurs in contravariant position in type A of value pet2 class Pets[+A](val pet:A) { def add(pet2: A): String = "done" } My understanding of this code snippet is that Pets is covariant and accepts objects that are subtypes of A. However, the function add takes in a parameter of type A

Why is Function[-A1,…,+B] not about allowing any supertypes as parameters?

守給你的承諾、 提交于 2019-11-27 17:31:54
I believe one can define covariance (at least, for objects) as 'the ability to use a value of a narrower (sub) type in place of a value of some wider (super) type', and that contravariance is the exact opposite of this. Apparently, Scala functions are instances of Function[-A1,...,+B] for contravariant parameter types A1, etc. and covariant return type, B. While this is handy for subtyping on Functions, shouldn't the above definition mean I can pass any supertypes as parameters? Please advise where I'm mistaken. Covariance and contravariance are qualities of the class not qualities of the

How to find the minimum covariant type for best fit between two types?

老子叫甜甜 提交于 2019-11-27 14:37:40
There's IsAssignableFrom method returns a boolean value indicates if one type is assignable from another type. How can we not only test if they are assignable from or to each other, but also know the minimum covariant type for best fit? Consider the following example(C# 4.0) Code // method body of Func is irrelevant, use default() instead Func<char[]> x = default(Func<char[]>); Func<int[]> y = default(Func<int[]>); Func<Array> f = default(Func<Array>); Func<IList> g = default(Func<IList>); g=x; g=y; y=x; // won't compile x=y; // won't compile // following two are okay; Array is the type for

Understanding Covariance and Contravariance in C# 4.0

烂漫一生 提交于 2019-11-27 12:10:50
I watched a video about it on Channel 9 but I didn't really understand it much. Can someone please give me a simple example about these that's easy to understand? After that maybe how it would be used in practice? You may want to look at this blog, he does a fantastic job of explaining it, but I think it will take more examples to clear it up for people, as this gets into a very hard-to-understand area, but, the quote below from the article sums it up well. http://hestia.typepad.com/flatlander/2008/12/c-covariance-and-contravariance-by-example.html "covariance and contravariance" means that