covariance

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

时间秒杀一切 提交于 2019-11-27 14:36:47
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 from object so I can't understand why this wouldn't work? The reason this is not legal is because it

What are the kinds of covariance in C#? (Or, covariance: by example)

给你一囗甜甜゛ 提交于 2019-11-27 14:02:57
问题 Covariance is (roughly) the ability to mirror inheritance of "simple" types in complex types that use them. E.g. We can always treat an instance of Cat as an instance of Animal . A ComplexType<Cat> may be treated as a ComplexType<Animal> , if ComplexType is covariant. I'm wondering: what are the "types" of covariance, and how do they relate to C# (are they supported?) Code examples would be helpful. For instance, one type is return type covariance , supported by Java, but not C#. I'm hoping

C++ covariant templates

北战南征 提交于 2019-11-27 13:38:30
I feel like this one has been asked before, but I'm unable to find it on SO, nor can I find anything useful on Google. Maybe "covariant" isn't the word I'm looking for, but this concept is very similar to covariant return types on functions, so I think it's probably correct. Here's what I want to do and it gives me a compiler error: class Base; class Derived : public Base; SmartPtr<Derived> d = new Derived; SmartPtr<Base> b = d; // compiler error Assume those classes are fully fleshed out... I think you get the idea. It can't convert a SmartPtr<Derived> into a SmartPtr<Base> for some unclear

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

Parameter type covariance in specializations

拈花ヽ惹草 提交于 2019-11-27 11:42:10
tl;dr What strategies exist to overcome parameter type invariance for specializations, in a language ( PHP ) without support for generics? Note: I wish I could say my understanding of type theory/safety/variance/etc., was more complete; I'm no CS major. Situation You've got an abstract class, Consumer , that you'd like to extend. Consumer declares an abstract method consume(Argument $argument) which needs a definition. Shouldn't be a problem. Problem Your specialized Consumer , called SpecializedConsumer has no logical business working with every type of Argument . Instead, it should accept a

Can I Override with derived types?

喜欢而已 提交于 2019-11-27 11:40:17
As far as i know it is not possible to do the following in C# 2.0 public class Father { public virtual Father SomePropertyName { get { return this; } } } public class Child : Father { public override Child SomePropertyName { get { return this; } } } I workaround the problem by creating the property in the derived class as "new", but of course that is not polymorphic. public new Child SomePropertyName Is there any solution in 2.0? What about any features in 3.5 that address this matter? Alex Lyman This is not possible in any .NET language because of type-safety concerns. In type-safe languages,

Calculating Covariance with Python and Numpy

 ̄綄美尐妖づ 提交于 2019-11-27 11:04:10
问题 I am trying to figure out how to calculate covariance with the Python Numpy function cov. When I pass it two one-dimentional arrays, I get back a 2x2 matrix of results. I don't know what to do with that. I'm not great at statistics, but I believe covariance in such a situation should be a single number. This is what I am looking for. I wrote my own: def cov(a, b): if len(a) != len(b): return a_mean = np.mean(a) b_mean = np.mean(b) sum = 0 for i in range(0, len(a)): sum += ((a[i] - a_mean) *

scala - Any vs underscore in generics

馋奶兔 提交于 2019-11-27 10:11:28
What is the different between the following Generics definitions in Scala: class Foo[T <: List[_]] and class Bar[T <: List[Any]] My gut tells me they are about the same but that the latter is more explicit. I am finding cases where the former compiles but the latter doesn't, but can't put my finger on the exact difference. Thanks! Edit: Can I throw another into the mix? class Baz[T <: List[_ <: Any]] OK, I figured I should have my take on it, instead of just posting comments. Sorry, this is going to be long, if you want the TL;DR skip to the end. As Randall Schulz said, here _ is a shorthand

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

丶灬走出姿态 提交于 2019-11-27 09:40:50
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 method (something which Java supports, IIRC) Robert Kozak Eric Lippert already answered this way

How to get around lack of covariance with IReadOnlyDictionary?

落花浮王杯 提交于 2019-11-27 09:01:54
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, or some other way to handle this? The options I can think of are: Hold two internal dictionaries and