covariance

Covariance beats concrete type?

二次信任 提交于 2019-12-07 09:46:24
问题 to be honest- ive asked (a part of this question) here but now i have a different - related question. public class Base { public void Foo(IEnumerable<string> strings) { } } public class Child : Base { public void Foo(IEnumerable<object> objects) { } } List<string> lst = new List<string>(); lst.Add("aaa"); Child c = new Child(); c.Foo(lst); (n C# 3 it will call : Base.Foo in C# 4 it will call : Child.Foo ) Im in FW4 ! , lets talk about it with all the respect to covariance : when I write c.Foo

Contravariant method argument type

浪子不回头ぞ 提交于 2019-12-07 09:16:18
问题 wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that. example: class AnimalShelter { Animal getAnimalForAdoption() { ... } void putAnimal(Animal animal) { ... } } class CatShelter extends AnimalShelter { @Overriding Cat getAnimalForAdoption() { return new Cat(); } @Overriding void putAnimal(Object animal) { … } } My

Func variance with multiple parameters

半世苍凉 提交于 2019-12-07 08:10:34
问题 Tried to something like this in our code but it fails: Func<Employee, Employee> _myFunc; void Main() { Func<Employee, Employee> test1 = _myFunc;//Ok Func<Employee, Person> test2 = _myFunc;//Ok Func<Person, Employee> test3 = _myFunc;//Fails Func<Person, Person> test4 = _myFunc;//Fails } public class Person { } public class Employee : Person { } The last two cases give this error: Cannot implicitly convert type System.Func<Employee, Employee> to System.Func<Person, Employee> . An explicit

How is the datatype of type parameter decided in covariance and contravariance?

好久不见. 提交于 2019-12-07 05:59:26
问题 I was reading the book Java Generics and Collections By Maurice Naftalin, Philip Wadler, and within the first two chapters I ended up in having my head messed up with doubts. I was not able to figure out the answers. In the call: public static <T> void copy(List<? super T> dst, List<? extends T> src) { for (int i = 0; i < src.size(); i++) { dst.set(i, src.get(i)); } } List<Object> objs = Arrays.<Object>asList(2, 3.14, "four"); List<Integer> ints = Arrays.asList(5, 6); Collections.copy(objs,

Array covariance in F#

为君一笑 提交于 2019-12-07 05:24:11
问题 Since .NET arrays are covariant, the following works in C#: var strArray = new string[0]; object[] objArray = strArray; In F#, given an array, 'T[] , what would be the best way to convert it to obj[] , without re-creating the array (e.g., Array.map box )? I'm using (box >> unbox) , but it feels sloppy. 回答1: box >> unbox seems like a good idea; O(1), and does the job, apparently. Consider also not using this CLR mis-feature. ;) 回答2: As Brian says, there's nothing wrong with box >> unbox ,

C# Generics Interface Covariance

给你一囗甜甜゛ 提交于 2019-12-07 05:19:26
问题 I'm not sure what's going on here but I'm getting a compiler error using the following code: namespace SO { interface IUser<PostType> { PostType Post { get; set; } } interface IPost<UserType> { UserType User { get; set; } } class User : IUser<Post> { //Implementation } class Post : IPost<User> { //Implementation } class SomeOtherClass { // Compiler Error: Cannot implicitly convert type 'SO.User' to // 'SO.IUser<SO.IPost<SO.User>>'. An explicit conversion exists // (are you missing a cast?)

Expression.Convert doesn't throw InvalidOperationException for invariant value type parameters?

只愿长相守 提交于 2019-12-07 04:19:13
问题 Expression.Convert generally throws in InvalidOperationException when "No conversion operator is defined between expression.Type and type." The return type parameter of Func<> is covariant for reference types. // This works. Func<SomeType> a = () => new SomeType(); Func<object> b = a; It isn't covariant for value types. Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type. //

Assigning IEnumerable (Covariance)

∥☆過路亽.° 提交于 2019-12-07 01:29:55
问题 Since IEnumerable has a covariant parameter in C# 4.0 I am confused how it is behaving in the following code. public class Test { IEnumerable<IFoo> foos; public void DoTestOne<H>(IEnumerable<H> bars) where H : IFoo { foos = bars; } public void DoTestTwo(IEnumerable<IBar> bars) { foos = bars; } } public interface IFoo { } public interface IBar : IFoo { } So basically the DoTestOne method doesn't compile while DoTestTwo does. In addition to why it doesn't work, if anyone knows how I can achieve

Covariance and Contravariance in C#

五迷三道 提交于 2019-12-06 18:35:05
问题 I will start by saying that I am Java developer learning to program in C#. As such I do comparisons of what I know with what I am learning. I have been playing with C# generics for a few hours now, and I have been able to reproduce the same things I know in Java in C#, with the exception of a couple of examples using covariance and contravariance. The book I am reading is not very good in the subject. I will certainly seek more info on the web, but while I do that, perhaps you can help me

Problem with Covariant return types from an abstract method

柔情痞子 提交于 2019-12-06 13:22:35
I’m trying to wrap up a two day beat down on Abstract methods and return type Covariance, I’ve already posted two similar questions and I am eternally grateful to the community for the info provided, I just need one last push to get to the finish line. Here is what I am trying to do: 2 abstract classes, RecruiterBase and CandidateBase, both have concreate implementations of RecruiterA and CandidateA. RecruiterBase has an abstract method to get all recruited candidates returning IQueryable. My implementation of RecruiterA overrides the GetCandidates() method to return IQueryable. public