Does C# support return type covariance?

UPDATE: This answer was written in 2011. After two decades of people proposing return type covariance for C# they have been implemented. See Covariant Returns in https://devblogs.microsoft.com/dotnet/c-9-0-on-the-record/. It sounds like what you want is return type covariance. C# does not support return type covariance. Return type covariance is where you override a base class method … Read more

Why is Scala’s immutable Set not covariant in its type?

Set is invariant in its type parameter because of the concept behind sets as functions. The following signatures should clarify things slightly: trait Set[A] extends (A=>Boolean) { def apply(e: A): Boolean } If Set were covariant in A, the apply method would be unable to take a parameter of type A due to the contravariance … Read more

Give examples of functions which demonstrate covariance and contravariance in the cases of both overloading and overriding in Java? [closed]

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 … Read more

What is a covariant return type?

Covariant return, means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method’s return type. To clarify this with an example, a common case is Object.clone() – which is declared to return a type of Object. You could override this in your … Read more

Why doesn’t the example compile, aka how does (co-, contra-, and in-) variance work?

Generically, a covariant type parameter is one which is allowed to vary down as the class is subtyped (alternatively, vary with subtyping, hence the “co-” prefix). More concretely: trait List[+A] List[Int] is a subtype of List[AnyVal] because Int is a subtype of AnyVal. This means that you may provide an instance of List[Int] when a … Read more

Co-variant array conversion from x to y may cause run-time exception

What it means is this Control[] controls = new LinkLabel[10]; // compile time legal controls[0] = new TextBox(); // compile time legal, runtime exception And in more general terms string[] array = new string[10]; object[] objs = array; // legal at compile time objs[0] = new Foo(); // again legal, with runtime exception In C#, … Read more

Why covariance and contravariance do not support value type

Basically, variance applies when the CLR can ensure that it doesn’t need to make any representational change to the values. References all look the same – so you can use an IEnumerable<string> as an IEnumerable<object> without any change in representation; the native code itself doesn’t need to know what you’re doing with the values at … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)