covariance

Any simple way to explain why I cannot do List<Animal> animals = new ArrayList<Dog>()? [duplicate]

烈酒焚心 提交于 2019-11-26 18:50:20
This question already has an answer here: Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? 16 answers I know why one shouldn't do that. But is there way to explain to a layman why this is not possible. You can explain this to a layman easily : Animal animal = new Dog(); . A dog is a kind of animal but a list of dogs is not a list of animals. Imagine you create a list of Dogs . You then declare this as List<Animal> and hand it to a colleague. He, not unreasonably , believes he can put a Cat in it. He then gives it back to you, and you now have a list of

Why can't I cast from a List<MyClass> to List<object>?

落爺英雄遲暮 提交于 2019-11-26 18:25:28
问题 I have a List of objects, which are of my type QuoteHeader and I want to pass this list as a list of objects to a method which is able to accept a List<object> . My line of code reads... Tools.MyMethod((List<object>)MyListOfQuoteHeaders); But I get the following error at design time... Cannot convert type 'System.Collections.Generic.List<MyNameSpace.QuoteHeader>' to 'System.Collections.Generic.List<object>' Do I need to do anything to my class to allow this? I thought that all classes inherit

Understanding Covariance and Contravariance in C# 4.0

旧巷老猫 提交于 2019-11-26 18:09:23
问题 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? 回答1: 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

Cast Generic<Derived> to Generic<Base>

丶灬走出姿态 提交于 2019-11-26 17:54:32
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 type BaseViewModel but of a derived type like BaseViewModel<Wire> or BaseViewModel<Connector> . How can I

Why does C#/CLR not support method override co/contra-variance?

给你一囗甜甜゛ 提交于 2019-11-26 17:51:53
问题 There are quite a few questions & answers about hacking around the limitation of C# not allowing method return (and argument) types to be changed to compatible types on overrides, but why does this limitation exist, either in the C# compiler or in the CLR? As I an see, there is nothing that could break if co/contra-variance was allowed, so what is the reasoning behind it? A similar question could be asked for widening access parameters - eg overriding a protected internal method with a public

How to get around lack of covariance with IReadOnlyDictionary?

天大地大妈咪最大 提交于 2019-11-26 17:47:52
问题 I'm trying to expose a read-only dictionary that holds objects with a read-only interface. Internally, the dictionary is write-able, and so are the objects within (see below example code). My problem is that IReadOnlyDictionary doesn't support covariant conversions because of the reason outlined in the question here. This means I can't just expose my internal dictionary as a read only one. So my question is, is there an efficient way to convert my internal dictionary to an IReadOnlyDictionary

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

倖福魔咒の 提交于 2019-11-26 16:52:09
问题 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

covariance in c#

爱⌒轻易说出口 提交于 2019-11-26 16:33:01
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 You can't do this, because it wouldn't be safe. Consider: List<Joe> joes = GetJoes(); List<Human>

c# covariant return types utilizing generics

杀马特。学长 韩版系。学妹 提交于 2019-11-26 16:19:41
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 ExistingEmployee I believe this code works fine, but it gets really nasty when I have several

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

孤街醉人 提交于 2019-11-26 15:56:03
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! 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 example: void mySub(List<Animal> myList) { myList.add(new Cat()); } If you were allowed to pass a List<Dog> to