covariance

Question about C# covariance

前提是你 提交于 2019-11-26 07:43:45
问题 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 that matter, I cannot even do the following: List<I1> listOfI12 = listOfCI1; Shouldn\'t covariance allow me to assign a derived type to a base type? 回答1: Simply

Why does C# (4.0) not allow co- and contravariance in generic class types?

走远了吗. 提交于 2019-11-26 06:46:39
问题 What is the real reason for that limitation? Is it just work that had to be done? Is it conceptually hard? Is it impossible? Sure, one couldn\'t use the type parameters in fields, because they are allways read-write. But that can\'t be the answer, can it? The reason for this question is that I\'m writing an article on variance support in C# 4, and I feel that I should explain why it is restricted to delegates and interfaces. Just to inverse the onus of proof. Update: Eric asked about an

Why can&#39;t I assign a List<Derived> to a List<Base>?

亡梦爱人 提交于 2019-11-26 06:45:06
问题 I defined the following class: public abstract class AbstractPackageCall { ... } I also define a subclass of this class: class PackageCall : AbstractPackageCall { ... } There are also several other subclases of AbstractPackageCall Now I want to make the following call: List<AbstractPackageCall> calls = package.getCalls(); But I always get this exception: Error 13 Cannot implicitly convert type \'System.Collections.Generic.List<Prototype_Concept_2.model.PackageCall>\' to \'System.Collections

covariance in c#

你。 提交于 2019-11-26 06:02:50
问题 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>; than it wouldn\'t be legal to do (joe)humans[0] because that item has been down casted.. and everyone would be happy. Now the only alternative is to create a new List

Cast Generic<Derived> to Generic<Base>

不羁的心 提交于 2019-11-26 05:39:40
问题 I have a base WPF UserControl that handles some common functionality for derived UserControls. In the code-behind of any derived UserControl I call an event private void SomeClick(object sender, RoutedEventArgs e) { HandleClick(sender); MyDataGrid.Items.Refresh(); } In my base UserControl I do public class BaseUserControl : UserControl { protected void HandleClick(object sender) { var vm = (BaseViewModel<Part>)DataContext; ... } } This throws an InvalidCastException since DataContext is of

Generics : List<? extends Animal> is same as List<Animal>?

非 Y 不嫁゛ 提交于 2019-11-26 04:11:37
问题 I am just trying to understand the extends keyword in Java Generics. List<? extends Animal> means we can stuff any object in the List which IS A Animal then won\'t the following also mean the same thing: List<Animal> Can someone help me know the difference between the above two? To me extends just sound redundant here. Thanks! 回答1: List<Dog> is a subtype of List<? extends Animal> , but not a subtype of List<Animal> . Why is List<Dog> not a subtype of List<Animal> ? Consider the following

c# covariant return types utilizing generics

女生的网名这么多〃 提交于 2019-11-26 03:57:19
问题 Is the code below the only way to implement covariant return types? public abstract class BaseApplication<T> { public T Employee{ get; set; } } public class Application : BaseApplication<ExistingEmployee> {} public class NewApplication : BaseApplication<NewEmployee> {} I want to be able to construct an Application or a NewApplication and have it return the appropriate Employee type from the Employee property. var app = new Application(); var employee = app.Employee; // this should be of type

C#: Overriding return types

好久不见. 提交于 2019-11-26 03:48:40
问题 Is there way to override return types in C#? If so how, and if not why and what is a recommended way of doing it? My case is that I have an interface with an abstract base class and descendants of that. I would like to do this (ok not really, but as an example!) : public interface Animal { Poo Excrement { get; } } public class AnimalBase { public virtual Poo Excrement { get { return new Poo(); } } } public class Dog { // No override, just return normal poo like normal animal } public class

Covariance, Invariance and Contravariance explained in plain English?

烈酒焚心 提交于 2019-11-26 03:46:24
问题 Today, I read some articles about Covariance, Contravariance (and Invariance) in Java. I read the English and German Wikipedia article, and some other blog posts and articles from IBM. But I\'m still a little bit confused on what these exactly are about? Some say it\'s about relationship between types and subtypes, some say it\'s about type conversion and some say it\'s used to decide whether a method is overridden or overloaded. So I\'m looking for an easy explanation in plain English, that

Why are arrays covariant but generics are invariant?

偶尔善良 提交于 2019-11-26 03:13:50
问题 From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There