covariance

Covariance and contravariance real world example

你说的曾经没有我的故事 提交于 2019-11-26 00:25:04
问题 I\'m having a little trouble understanding how I would use covariance and contravariance in the real world. So far, the only examples I\'ve seen have been the same old array example. object[] objectArray = new string[] { \"string 1\", \"string 2\" }; It would be nice to see an example that would allow me to use it during my development if I could see it being used elsewhere. 回答1: Let's say you have a class Person and a class that derives from it, Teacher. You have some operations that take an

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

冷暖自知 提交于 2019-11-26 00:14:37
问题 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, List<string> can be assigned to List<object> . How could that be? In Jon Skeet\'s book C# in Depth , it is explained why C# generics doesn\'t support covariance and contra-variance. It is mainly for writing secure code. Now, C# 4.0 changed to support them. Would it bring chaos? Anybody know the details about C# 4.0 can give some explanation? 回答1: Variance will

Difference between Covariance & Contra-variance

折月煮酒 提交于 2019-11-25 23:24:04
问题 I am having trouble understanding the difference between covariance and contravariance. 回答1: The question is "what is the difference between covariance and contravariance?" Covariance and contravariance are properties of a mapping function that associates one member of a set with another . More specifically, a mapping can be covariant or contravariant with respect to a relation on that set. Consider the following two subsets of the set of all C# types. First: { Animal, Tiger, Fruit, Banana }.

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

﹥>﹥吖頭↗ 提交于 2019-11-25 22:34:34
问题 It seems that a List object cannot be stored in a List variable in C#, and can\'t even be explicitly cast that way. List<string> sl = new List<string>(); List<object> ol; ol = sl; results in Cannot implicitly convert type System.Collections.Generic.List<string> to System.Collections.Generic.List<object> And then... List<string> sl = new List<string>(); List<object> ol; ol = (List<object>)sl; results in Cannot convert type System.Collections.Generic.List<string> to System.Collections.Generic

Does C# support return type covariance?

本秂侑毒 提交于 2019-11-25 22:30:44
问题 I\'m working with the .NET framework and I really want to be able to make a custom type of page that all of my website uses. The problem comes when I am trying to access the page from a control. I want to be able to return my specific type of page instead of the default page. Is there any way to do this? public class MyPage : Page { // My own logic } public class MyControl : Control { public MyPage Page { get; set; } } 回答1: It sounds like what you want is return type covariance. C# does not

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

此生再无相见时 提交于 2019-11-25 22:18:02
问题 Look at the following example (partially taken from MSDN Blog): class Animal { } class Giraffe : Animal { } static void Main(string[] args) { // Array assignment works, but... Animal[] animals = new Giraffe[10]; // implicit... List<Animal> animalsList = new List<Giraffe>(); // ...and explicit casting fails List<Animal> animalsList2 = (List<Animal>) new List<Giraffe>(); } Is this a covariance problem? Will this be supported in the future C# release and are there any clever workarounds (using

Why covariance and contravariance do not support value type

£可爱£侵袭症+ 提交于 2019-11-25 21:59:11
问题 IEnumerable<T> is co-variant but it does not support value type, just only reference type. The below simple code is compiled successfully: IEnumerable<string> strList = new List<string>(); IEnumerable<object> objList = strList; But changing from string to int will get compiled error: IEnumerable<int> intList = new List<int>(); IEnumerable<object> objList = intList; The reason is explained in MSDN: Variance applies only to reference types; if you specify a value type for a variant type

Convert List<DerivedClass> to List<BaseClass>

社会主义新天地 提交于 2019-11-25 21:54:24
问题 While we can inherit from base class/interface, why can\'t we declare a List<> using same class/interface? interface A { } class B : A { } class C : B { } class Test { static void Main(string[] args) { A a = new C(); // OK List<A> listOfA = new List<C>(); // compiler Error } } Is there a way around? 回答1: The way to make this work is to iterate over the list and cast the elements. This can be done using ConvertAll: List<A> listOfA = new List<C>().ConvertAll(x => (A)x); You could also use Linq: