//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 = delegate(int x, int y, int z) { return((x + y + z) / 3); }; Console.WriteLine(delA(5, 5, 27)); Console.ReadKey(); }
static void Main(string[] args) { DelegateA delA = MethodA; DelegateB delB = MethodA; // Contravariance //DelegateA delA = new DelegateA( MethodA ); //DelegateB delB = new DelegateB( MethodA ); // Contravariance }
public static void Simple_Delegates() { 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); }
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(); }
public static void Test(TestDelegate obj) { DelegateA arg = null; arg(1.0f); float upvalue = 0; arg = arg2 => { upvalue = arg2; }; arg(2.0f); obj.e += arg => { }; Func <float, float> asd = arg2 => arg2; Func <int, float> aaa = StaticFunc; }
public void AddToTreeViewSafely(TreeNodeCollection ncParent, TreeNode nchild, bool checkIfAlreadyHasChildNode) { if (MainForm.DefInstance.InvokeRequired) { DelegateA dA = new DelegateA(AddToTreeViewSafely); MainForm.DefInstance.Invoke(dA, new object[] { ncParent, nchild, checkIfAlreadyHasChildNode }); return; } if (checkIfAlreadyHasChildNode) { foreach (TreeNode n in ncParent) { if (n.Text == nchild.Text) { return; } } } ncParent.Add(nchild); }
public void AddToTreeViewSafely(TreeNodeCollection ncParent, TreeNode nchild, bool checkIfAlreadyHasChildNode) { if (MainForm.DefInstance.InvokeRequired) { DelegateA dA = new DelegateA(AddToTreeViewSafely); MainForm.DefInstance.Invoke(dA, new object[] { ncParent, nchild, checkIfAlreadyHasChildNode }); return; } if (checkIfAlreadyHasChildNode) foreach (TreeNode n in ncParent) if (n.Text == nchild.Text) return; ncParent.Add(nchild); }
static bool TestCompare(DelegateA a, DelegateB b) { return(a != b); }
static bool TestCompare (DelegateA a, DelegateB b) { return a != b; }