Ejemplo n.º 1
0
        //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));
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            DelegateA delA = MethodA;
            DelegateB delB = MethodA;   // Contravariance

            //DelegateA delA = new DelegateA( MethodA );
            //DelegateB delB = new DelegateB( MethodA ); // Contravariance
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
 static bool TestCompare(DelegateA a, DelegateB b)
 {
     return(a != b);
 }
Ejemplo n.º 10
0
	static bool TestCompare (DelegateA a, DelegateB b)
	{
		return a != b;
	}