Why does the .NET 4 variance for generic type arguments also not apply to classes?

Possible duplicates:
Why is there no general variance in C # 4.0? Why does C # (4.0) not allow matches and comparisons in generic types?

The new joint and contravariant compatibility of .NET 4.0 for arguments of a universal type works only for interfaces and delegates. What is the reason not to support it for classes too?

+3
source share
2 answers

For type safety, C # 4.0 supports covariance / contravariance ONLY for type parameters marked in or out .

, , . , CLR . , :

public class Stack<T>
{
  int position;
  T[] data = new T[100];
  public void Push (T obj)   { data[position++] = obj;  }
  public T Pop()             { return data[--position]; }
}

, , . , - # / .

. : Stack :

public interface IPoppable<out T> { T Pop(); }
public interface IPushable<in T> { void Push (T obj); }

, T IPoppable IPushable. , T , - , IPoppable IPushable.

, / , , , . , , . Enumerable - ?

+8

.NET # VB.NET , , , . , - , 99,9999% , , .

, / co- contravariance (, "in" / "out" ) . , - - .

6 .net, ?


- .net

  • / -
  • co-
+1

Source: https://habr.com/ru/post/1767134/


All Articles