.
// , . delegate AnotherOp<out , V>(V obj);
, , , . V .
.
// // .
Using System;
// , . delegate bool SomeOpcin >( obj);
// , . delegate AnotherOpCout , V>(V obj);
class Alpha {
public int Val { get; set; }
public Alpha(int v) { Val = v; }
}
class Beta: Alpha {
public Beta (int v): base (v) { }
}
class GenDelegateVarianceDemo {
// true, // obj.Val , static bool IsEven(Alpha obj) {
if((obj.Val % 2) == 0) return true; return false;
}
static Beta Changelt(Alpha obj) { return new Beta(obj.Val +2);
}
static void Main() {
Alpha objA = new Alpha(4);
Beta objB = new Beta(9);
// .
// SomeOp<Alpha> IsEven. SomeOp<Alpha> checklt = IsEven;
// SomeOp<Beta>.
SomeOp<Beta> checklt2;
// - SomeOp<Alpha> SomeOp<Beta>.
// *** . *** checklt2 = checklt;
// .
Console.WriteLine(checklt2(objB));
// , .
// AnotherOp.
// Beta,
// Alpha.
// , modifylt // Changelt.
AnotherOp<Beta, Alpha> modifylt = Changelt;
// Alpha,
// Alpha.
AnotherOp<Alpha, Alpha> modifyIt2;
// modifylt modifyIt2.
// *** . *** modifyIt2 = modifylt;
// . objA = modifyIt2(objA);
Console.WriteLine(objA.Val);
}
}
.
False
. , SomeOp and AnotherOp in out . - .
|
|
, : ? : . , C# : . .
MSIL, . , JIT- , . .
, Gen<T>, Gen<T> . , , , . , .
, , , int double. , Gen<int> Gen, Gen<double> Gen, . , , , ( ). .
,