Exemple #1
0
        public int callDelegate2(TestDelegate2 del)
        {
            int a = 3;
            int b = del(2, out a);

            return(a + b);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // Original delegate syntax required
            // initialization with a named method.
            TestDelegate testDelA = new TestDelegate(M);

            // C# 2.0: A delegate can be initialized with
            // inline code, called an "anonymous method." This
            // method takes a string as an input parameter.
            TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

            // C# 3.0. A delegate can be initialized with
            // a lambda expression. The lambda also takes a string
            // as an input parameter (x). The type of x is inferred by the compiler.
            TestDelegate testDelC = (x) => { Console.WriteLine(x); };

            // Invoke the delegates.
            testDelA("This is TestDelA");
            testDelB("This is TestDelB");
            testDelC("This is TestDelC");

            TestDelegate2 testDelD = x => x * x;

            Console.WriteLine(testDelD(12));

            Console.ReadKey();
        }
        private static void lambda()
        {
            // () => expression
            TestDelegate2 lambdaDel = (x) => x * x;

            Console.WriteLine(lambdaDel(5)); // 25

            Func <int, bool> myFunc = (x) => x == 5;

            Console.WriteLine(myFunc(4)); // false
        }
        public void TestAlternateConstructor()
        {
            var called = new AtomicBoolean(false);
            var dele   = new TestDelegate2(called);

            adapter = new MessageListenerAdapter(null, dele, "MyPojoMessageMethod");
            var bytes = EncodingUtils.GetDefaultEncoding().GetBytes("foo");

            adapter.OnMessage(Message.Create(bytes, messageProperties), null);
            Assert.True(called.Value);
        }
Exemple #5
0
        public static void Excute()
        {
            TestDelegate2 td = Double;
            TestDelegate  f  = D;

            Console.WriteLine(td(4, 7));
            f("44147");

            anoy d = (m, n) => { return(m + n); };

            Console.Write(d("g", "rr"));
        }
Exemple #6
0
        static void Main(string[] args)
        {
            TestDelegate testDelegate = new TestDelegate(MethodOne);

            testDelegate += MethodTwo;
            testDelegate += MethodThree;
            testDelegate -= MethodTwo;
            testDelegate.Invoke("Isaac");

            TestDelegate2 testDelegate2 = new TestDelegate2(MethodOne);

            testDelegate2.Invoke("Mpho", 20);
        }
Exemple #7
0
 public int callDelegate2(TestDelegate2 del)
 {
     int a = 3;
     int b = del(2, out a);
     return a + b;
 }
 private void TestFun2(TestDelegate2 <string, int> de)
 {
     de("test", 1000);
 }
 // Use this for initialization
 void Start()
 {
     mDelegate1 = new TestDelegate1(StaticDelegateFun);
     mDelegate2 = new TestDelegate2 <string, int>(StaticDelegateFun);
 }
Exemple #10
0
    static void Main(string[] args)
    {
        // Generic Action
        Action<string, ConsoleColor, int> actionTarget =
            new Action<string, ConsoleColor, int>(ActionFuncDel.DisplayMessage);
        actionTarget("yo action", ConsoleColor.Yellow, 5);

        Func<int, int, int> funcTarget1 =
            new Func<int, int, int>(ActionFuncDel.Add);
        int ret = funcTarget1(70, 80);
        Console.WriteLine("Func<int,int,int> ret = {0}", ret);

        Func<int, int, string> funcTarget2 =
            new Func<int, int, string>(ActionFuncDel.SumToString);
        string strRet = funcTarget2(90, 300);
        Console.WriteLine("Func<int,int,string> ret = {0}", strRet);

        // Generic Delegate demo
        GenDel.TestGenDelegate();

        TestCar();

        TestMath();

        TestDelegate1 td1 = new TestDelegate1();
        td1.TestNumberChanger();
        TestDelegate2 td2 = new TestDelegate2();
        int[] iArray = {3,5,1,3,8};

        td2.TestCompareInt(TestDelegate2.SortType.Ascending, iArray);
        td2.TestCompareInt(TestDelegate2.SortType.Descending, iArray);
    }
Exemple #11
0
        static void Main(string[] args)
        {
            TestDelegate td = null;



            td = new TestDelegate(Giants);

            td += Jets;

            td();

            TestDelegate td2 = null;

            td2  = new TestDelegate(Giants);
            td2 += Jets;

            Z(td2);


            Team t = new Team();

            t.m_Name = "Boys";

            TestDelegate td3 = new TestDelegate(t.ShowName);

            td3();

            t.m_Name = "Girls";
            td();


            TestDelegate2 td4 = new TestDelegate2(Show);

            td4("C# is awesome");

            // no parameter version
            Action ad1 = null;

            ad1 = new Action(Giants);
            ad1();

            // one parameter version
            Action <string> ad2 = null;

            ad2 = new Action <string>(Show);
            ad2("C# is awesome");

            //Action<string, int> ad3 = null;
            //ad3 = new Action<string, int>(ShowTwo);

            Func <int> fd = null;

            fd = new Func <int>(GetNum);
            int num = fd(); // Calls GetNum method

            Func <string, int> fd2 = null;

            fd2 = new Func <string, int>(ConvertStringToNum);
            int num2 = fd2("100");

            Action <string> anonDel = delegate(string s)
            {
                Console.WriteLine(s);
            };

            Action <string> annonDel = s =>
            {
                Console.WriteLine(s);
            };

            annonDel("WOoow");

            Func <int, int> f = y =>
            {
                return(y * y);
            };

            int sqNum;

            sqNum = f(20);
            Console.WriteLine(sqNum);



            anonDel("Go mom!");


            Secretary s = new Secretary();

            Employee e1 = new Employee();
            Employee e2 = new Employee();

            e1.name = "Jane";
            e2.name = "Bob";

            s.CallEvent += e1.HandlePhoneCall;
            s.CallEvent += e2.HandlePhoneCall;

            s.FireCallEvent();

            Console.WriteLine("Remove e1 as listener...");
            s.CallEvent -= e1.HandlePhoneCall;
            s.FireCallEvent();

            void Giants()
            {
                Console.WriteLine("Go Giants");
            }

            void Jets()
            {
                Console.WriteLine("Go Jets");
            }

            void Cowboys()
            {
                Console.WriteLine("Go Cowboys");
            }

            Console.ReadLine();

            // method that takes a delegate as a parameter

            void Z(TestDelegate e)
            {
                // the delegate will run both giants and jets

                e();
            }

            void Show(string displayString)
            {
                Console.WriteLine(displayString);
            }

            int GetNum()
            {
                return(Convert.ToInt32(Console.ReadLine()));
            }
        }