, in out:
public interface ISomeInterface<in T> where T : SomeBaseClass{
}
:
public interface ISomeInterface<out T> where T : SomeBaseClass{
}
, in T , out, T .
:
, X<S> X<B>.
:
, X<B> X<S>.
- S - , B - .
, , # 4.0, .
class Stack<T>{
int i;
T[] array = new T[1000];
public void Push(T element){
array[i++] = element;
}
}
class BaseClass{
}
class SubClass : BaseClass{
}
Infact , , Stack :
interface IPushable<in T>{
void Push(T element);
}
:
IPushable<BaseClass> stackB = new Stack<BaseClass>();
IPushable<SubClass> stackS = stackB;
stackS.Push(new SubClass());
, Stack :
interface IPoppable<in T>{
T Pop();
}
, :
IPoppable<SubClass> stackS = new Stack<SubClass>();
IPoppable<BaseClass> stackB = stackB;
BaseClass baseClass = stackB.Pop();
, - .