//Contrvariance in Arugments public static void Example() { DelegateA delegateA = HandleDemoEvent; // No variavnce DelegateB delegateB = HandleDemoEvent; // Contravariance DelegateC delegateC = HandleDemoEvent; // Contravariance //ExampleVarianceDelegate.DelegateB delegateB1 = HandleDemoEventC; // Exception //ExampleVarianceDelegate.DelegateC delegateC1 = HandleDemoEventB; // Exception //ExampleVarianceDelegate.DelegateC delegateA1 = HandleDemoEventB; // Covariance doesn't work Exception delegateA("", new EventArgs()); //delegateB("", new EventArgs()); //Exception, you can't call it with EventArgs //delegateC("", new EventArgs()); //Exception, you can't call it with EventArgs delegateB("", new KeyPressEventArgs('b')); delegateC("", new MausePressEventArgs(new Object(), 1, 1, 1, 1)); // EVENTS The same rule riseA += HandleDemoEvent; riseB += HandleDemoEvent; //Contravariance riseC += HandleDemoEvent; //Contravariance //riceC += HandleDemoEventB; //Exceptoion //riceB += HandleDemoEvent; // Covariance Exception riseA.Invoke("", new EventArgs()); riseB.Invoke("", new KeyPressEventArgs('b')); riseC.Invoke("", new MausePressEventArgs(new Object(), 1, 1, 1, 1)); }
static void Main(string[] args) { DelegateA delA = MethodA; DelegateB delB = MethodA; // Contravariance //DelegateA delA = new DelegateA( MethodA ); //DelegateB delB = new DelegateB( MethodA ); // Contravariance }
static void Main(string[] args) { del += new DelegateB(India); del += new DelegateB(USA); del += new DelegateB(UK); del.Invoke(); Console.ReadLine(); }
public static void Sample1() { DelegateA delegateA = new DelegateA(Square); DelegateB delegateB = Increment; var resultA = delegateA(3); var resultB = delegateB(3); Console.WriteLine(" Square: {0}", resultA); Console.WriteLine("Increment: {0}", resultB); Console.ReadLine(); }
static bool TestCompare(DelegateA a, DelegateB b) { return(a != b); }
static bool TestCompare (DelegateA a, DelegateB b) { return a != b; }