contravariance

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

天大地大妈咪最大 提交于 2019-11-26 09:06:14
问题 I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it\'s only another word for polymorphism . Am I correct with the above statement? Or have I got it wrong ? 回答1: It's certainly related to polymorphism. I wouldn't say they're just "another word" for polymorphism though - they're about very specific situations, where you can treat one type as if it were another type

Casting List<T> - covariance/contravariance problem

点点圈 提交于 2019-11-26 09:03:53
问题 Given the following types: public interface IMyClass { } public class MyClass : IMyClass { } I wonder how can I convert a List<MyClass> to a List<IMyClass> ? I am not completely clear on the covariance/contravariance topics, but I understand that I cannot just plainly cast the List because of that. I could come up with this trivial solution only; lacking any elegance, wasting resources: ... public List<IMyClass> ConvertItems(List<MyClass> input) { var result = new List<IMyClass>(input.Count);

Contravariance explained

淺唱寂寞╮ 提交于 2019-11-26 08:58:41
问题 First of, I have read many explanations on SO and blogs about covariance and contravariance and a big thanks goes out to Eric Lippert for producing such a great series on Covariance and Contravariance. However I have a more specific question that I am trying to get my head around a little bit. As far as I understand per Eric\'s explanation is that Covariance and Contravariance are both adjectives that describe a transformation. Covariant transformation is that which preserves the order of

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

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

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

Demonstrate covariance and contravariance in Java? [closed]

。_饼干妹妹 提交于 2019-11-26 02:50:46
Please show a good example for covariance and contravariance in Java. Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because it takes a parameter of a superclass of the parameter of Super#doSomething (but, again, fullfills the

How do I return a reference to something inside a RefCell without breaking encapsulation?

谁说胖子不能爱 提交于 2019-11-26 02:37:57
问题 I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let

Demonstrate covariance and contravariance in Java? [closed]

别说谁变了你拦得住时间么 提交于 2019-11-26 01:51:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Please show a good example for covariance and contravariance in Java. 回答1: Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething

Understanding Covariant and Contravariant interfaces in C#

ぃ、小莉子 提交于 2019-11-26 01:35:18
问题 I\'ve come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context. Is there a good concise explanation of what they are and what they are useful for out there? Edit for clarification: Covariant interface: interface IBibble<out T> . . Contravariant interface: interface IBibble<in T> . . 回答1: With <out T> , you can treat the interface reference as one upwards in the hierarchy. With <in T> , you can treat the interface