コード例 #1
0
        static void Main(string[] args)
        {
            #region Instantiating a delegate with methods
            // SayDelegate is like a type for methods
            Console.WriteLine("----------------Standard delegate calls---------------");
            SayDelegate hello = new SayDelegate(SayHello);
            SayDelegate bye   = new SayDelegate(SayBye);
            SayDelegate wow   = new SayDelegate(x => Console.WriteLine($"WOW {x}!"));
            //SayDelegate broken = new SayDelegate((x, y) => Console.WriteLine($"{x} and {y}")); // This will not work since there are 2 parameters instead of 1

            hello("Bob");
            bye("Bob");
            wow("Bob");
            #endregion
            #region Passing a method to a delegate parameter ( Higher order method )
            Console.WriteLine("----------------Higher order method calls---------------");
            SayWhatever("Bob", SayHello);
            SayWhatever("Bob", SayBye);
            SayWhatever("Bob", x => Console.WriteLine($"WOW {x}!"));
            SayWhatever("Bob", x =>
            {
                Console.WriteLine($"This is {x}!");
                Console.WriteLine("They are cool!");
            });

            #endregion

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            #region Instatiating a delegate with methods
            SayDelegate hello = new SayDelegate(SayHello);
            SayDelegate bye   = new SayDelegate(SayBye);


            //using a delegate (delegates point to methods)
            hello("Martin");
            bye("Martin");

            hello("Dejan");
            bye("Dejan");



            #endregion

            #region Passing method to an input param of type delegate parameter
            Thread.Sleep(1500);
            SayWhatever("Ivo", SayHello);
            SayWhatever("Bob", SayBye);

            Thread.Sleep(2000);
            //send method as input param to other m,ethod by using lambda expression (anon method)
            SayWhatever("John", x => Console.WriteLine($"Wow {x}"));

            ////send method as input param to other m,ethod by using lambda expression (anon method) with own scope
            SayWhatever("Ann", x =>
            {
                if (x == "Ann")
                {
                    Console.WriteLine($"Ohh {x}, it's you");
                }
                else
                {
                    Console.WriteLine($"Stuff with {x}");
                    Console.WriteLine($"Other stuff with {x}");
                }
            });
            #endregion

            #region Making a higher order function
            Console.WriteLine("NUMBER MASTER! IT IS REALLY MASTER AT NUMBERS!");

            NumberMaster(10, 20, (x, y) => x + y);
            NumberMaster(134, 112, (x, y) => x - y);
            NumberMaster(2, 5, (x, y) => 0);
            NumberMaster(22, 33, (x, y) => {
                if (x > y)
                {
                    return(y);
                }
                return(x);
            });
            #endregion


            Console.ReadLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: kpm563/BizRuntime_Training
        /// <summary>
        /// This is Main method of the class which takes only one arguement as string
        /// </summary>
        /// <param name="args">This is only parameter which must be an string type.</param>
        static void Main(string[] args)
        {
            log4net.Config.BasicConfigurator.Configure();

            Program program = new Program();

            log.Info("Normal method call ");
            log.Info("Sum of the entered numbers :: " + program.Add(30, 20));
            log.Info(SayHello("Sachin"));
            Console.WriteLine();

            log.Info("Usage of delegates...");
            //Creating delegate instance
            AddDelegate addDelegate = new AddDelegate(program.Add);
            SayDelegate sayDelegate = new SayDelegate(SayHello);

            log.Info("Method call using delegates.");
            log.Info("Addition :: " + addDelegate(50, 40));
            log.Info(sayDelegate("Samir"));
            Console.WriteLine();

            log.Info("Add :: " + addDelegate.Invoke(89, 68));
            log.Info(sayDelegate.Invoke("Rahul"));

            Console.Read();
        }
コード例 #4
0
ファイル: DelegateDemo.cs プロジェクト: Anil1111/DotNet
    static void Main()
    {
        // [A] Hi 함수를 say 이름으로 대신해서 호출
        SayDelegate say = Hi;

        say();
    }
コード例 #5
0
        public static void ChatBotSays(string whatever, SayDelegate sayDelegate)
        {
            Console.WriteLine("Chatbot says:");
            string result = sayDelegate(whatever);

            Console.WriteLine(result);
        }
コード例 #6
0
        static void Main(string[] args)
        {
            SayDelegate del = new SayDelegate(SayHello);
            SayDelegate bye = new SayDelegate(SayBye);
            SayDelegate wow = new SayDelegate(x => Console.WriteLine($"Wow {x}!"));

            //del("Bob");
            //bye("Jill");
            //wow("Greg");
            SayWhatever("Bob", SayHello);
            SayWhatever("Jill", SayBye);
            SayWhatever("Greg", x => Console.WriteLine($"Wow {x}!"));
            SayWhatever("Anne", x =>
            {
                Console.WriteLine($"Stuff with {x}");
                Console.WriteLine($"Other stuff with {x}");
            });
            Console.WriteLine("NUMBER MASTER! IT IS REALLY MASTER AT NUMBERS!");
            NumberMaster(2, 5, (x, y) => x + y);
            NumberMaster(2, 5, (x, y) => x - y);
            NumberMaster(2, 5, (x, y) => 0);
            NumberMaster(2, 5, (x, y) =>
            {
                if (x > y)
                {
                    return(x);
                }
                return(y);
            });
            Console.ReadLine();
        }
コード例 #7
0
        static void Main(string[] args)
        {
            SayDelegate say     = new SayDelegate(StringMagic);
            SayDelegate compare = new SayDelegate((string s, string l) =>
            {
                if (s.Length > l.Length)
                {
                    return(true);
                }
                return(false);
            });
            SayDelegate firstSameChar = new SayDelegate((string str1, string str2) =>
            {
                if (str1[0] == str2[0])
                {
                    return(true);
                }
                return(false);
            });
            SayDelegate lastSameChar = new SayDelegate((string str1, string str2) =>
            {
                if (str1[str1.Length - 1] == str2[str2.Length - 1])
                {
                    return(true);
                }
                return(false);
            });

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

            say("hello", "viktor");
            compare("hellllllo", "hello");
            firstSameChar("kniga", "kokoska");
            lastSameChar("petko", "trajko");
        }
コード例 #8
0
        static void Main(string[] args)
        {
            //instance
            Program p = new Program();

            p.AddNums(1, 2);

            //static
            Console.WriteLine(Program.StringHello("arun"));

            //2
            AddDelegate addDelegate = new AddDelegate(p.AddNums);
            SayDelegate sayDelegate = new SayDelegate(StringHello);


            Console.WriteLine("Delegates");
            addDelegate(2, 4);
            addDelegate.Invoke(10, 20);
            Console.WriteLine(sayDelegate("ARUN"));
            Console.WriteLine(sayDelegate.Invoke("Arun"));


            Console.WriteLine("Multi cast delegates");
            HelloDelegate helloDelegate = p.SayHello;

            helloDelegate += p.SayHelloWorld;
            helloDelegate();
        }
コード例 #9
0
 static void Main(string[] args)
 {
     // 암시적으로 변수할당(var)은 무명메서드 사용 불가
     // 콜백메서드를 선언안해줘도 됨
     SayDelegate say = delegate()
     {
         Console.WriteLine("안녕");
     };
 }
コード例 #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter program no.to execute");
            int i = Convert.ToInt32(Console.ReadLine());

            switch (i)
            {
            case 1:
                ClsDelegates obj1 = new ClsDelegates();
                //obj1.Add(25,30);
                //ClsDelegates.SayHello("anji");
                AddDelegate ad = new AddDelegate(obj1.Add);
                ad.Invoke(10, 20);
                SayDelegate sd = new SayDelegate(ClsDelegates.SayHello);
                sd.Invoke("anji");
                break;

            case 2:
                ClsMultiCastDelegates obj2 = new ClsMultiCastDelegates();
                RectDelegate          rd   = obj2.GetArea;
                rd += obj2.GetParameter;
                rd.Invoke(30.5, 20.5);
                break;

            case 3:
                // ClsAnonymous obj3 = new ClsAnonymous();
                //DelGreetings dg = new DelGreetings(obj3.Greetings);
                //dg(" anji");
                DelGreetings dg = delegate(string name)
                {
                    Console.WriteLine("Hello" + name + " a very good morning");
                };
                dg(" anji");
                break;

            case 4:
                //DelGreetings dg1 = delegate (string name)
                //{
                //    Console.WriteLine("Hello" + name + " a very good morning");
                //};
                DelGreetings dg1 = (name) =>
                {
                    Console.WriteLine("Hello" + name + " a very good morning");
                };
                dg1(" anji");
                break;

            case 5:
                ClsGenericDelegate.CreateDelegate();
                break;

            default:
                break;
            }
            Console.Read();
        }
コード例 #11
0
    static void Main()
    {
        // delegate 키워드로 함수를 바로 정의해서 사용
        SayDelegate say = delegate()
        {
            Console.WriteLine("반갑습니다.");
        };

        say();
    }
コード例 #12
0
ファイル: Form1.cs プロジェクト: tkhsysk/ThreadTest
        /// <summary>
        /// コールバック関数
        /// </summary>
        /// <param name="ar"></param>
        public void SayComplete(IAsyncResult ar)
        {
            SayDelegate d           = (SayDelegate)((AsyncResult)ar).AsyncDelegate;
            string      returnValue = d.EndInvoke(ar);

            // callbackに渡されるパラメータ
            DateTime now = (DateTime)ar.AsyncState;

            // say "ok"
            Debug.WriteLine($"{returnValue} {now.ToString()}");
        }
コード例 #13
0
ファイル: Form1.cs プロジェクト: tkhsysk/ThreadTest
        /// <summary>
        /// delegateを使ってスレッドを起動
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            // delegateの作成
            SayDelegate d = new SayDelegate(Say);

            // BeginInvokeの呼び出し
            IAsyncResult ar = d.BeginInvoke("hello", new AsyncCallback(SayComplete), DateTime.Now);

            // 参考サイト:
            // http://www.atmarkit.co.jp/ait/articles/0504/20/news111_3.html
        }
コード例 #14
0
    static void Main()
    {
        //[A] Hi 함수를 say 이름으로 대신해서 호출
        SayDelegate say = Hi;

        say();

        //[B] Hi 함수를 hi 이름으로 대신해서 호출: 또 다른 모양
        var hi = new SayDelegate(Hi);

        hi();
    }
コード例 #15
0
        static void Main(string[] args)
        {
            Program2    p  = new Program2();
            AddDelegate ad = new AddDelegate(p.AddNum);

            ad(10, 30);
            SayDelegate sd  = new SayDelegate(p.SayHello);
            String      str = sd("anil");

            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #16
0
    public SayDelegate sayDelegate;               //SayDelegate宣告sayDelegate來存放函數

    private void Start()
    {
        sayDelegate = Bill;  //指定Bill函數給sayDelegate
        sayDelegate("Hi ");
        sayDelegate = Alice; //指定Alice函數給sayDelegate
        sayDelegate("Hi ");

        sayDelegate  = null;  //清空sayDelegate裡面存放的函數
        sayDelegate += Bill;  //新增Bill到sayDelegate
        sayDelegate += Alice; //再新增Alice到sayDelegate
        sayDelegate("Hello");
    }
コード例 #17
0
        static void Main(string[] args)
        {
            Delegates2  d  = new Delegates2();
            AddDelegate ad = new AddDelegate(d.AddNums);

            ad(10, 20);
            SayDelegate sd = new SayDelegate(Delegates2.SayHello);
            string      st = sd("world2");

            Console.WriteLine(st);
            ad.Invoke(10, 80);
            Console.Read();
        }
コード例 #18
0
ファイル: DeleExp0.cs プロジェクト: singhJeetu/bizzz
        static void Main(string[] args)
        {
            DeleExp0    d  = new DeleExp0();
            AddDelegate ad = new AddDelegate(d.AddNums);

            ad(100, 50);
            //Or
            //ad.Invoke(100, 50);
            SayDelegate sd  = new SayDelegate(DeleExp0.SayHello);
            string      str = sd("jeet"); //or sd.Invoke("jeet");

            Console.WriteLine(str);
        }
コード例 #19
0
        static void Main(string[] args)
        {
            DelegateSingle p  = new DelegateSingle();
            AddDelegate    a1 = new AddDelegate(p.AddNum);

            a1(100, 200);
            a1(15, 30);
            SayDelegate sd = new SayDelegate(SayHello);
            //string str = DelegateSingle.SayHello("Ankur");
            String str = sd("Raju");

            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #20
0
        static void Main(string[] args)
        {
            SayDelegate del = new SayDelegate(SayHello);
            SayDelegate by  = new SayDelegate(SayBy);
            SayDelegate wow = new SayDelegate(x => Console.WriteLine($"wow {x}"));

            del("Bob");
            by("Jill");
            wow("John Doe");
            SayWhatever("Bob", SayHello);
            NumberMaster(2, 5, (x, y) => x + y);
            NumberMaster(2, 5, (x, y) => x - y);
            Console.ReadLine();
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: eci4ever/csharpbasic
        static void Main(string[] args)
        {
            Program pr = new Program();

            AddDelegate ad = new AddDelegate(pr.AddNum);

            ad.Invoke(10, 20);
            ad(20, 30);
            SayDelegate sd = new SayDelegate(SayHello);

            Console.WriteLine(sd.Invoke("Test"));
            Console.WriteLine(sd("Test2"));
            // pr.AddNum(5,3);
            // Console.WriteLine(SayHello("nmfairus"));
            // Console.WriteLine("Hello World!");
        }
コード例 #22
0
ファイル: Class1.cs プロジェクト: sankritdivyanshu/Bizruntime
        static void Main(string[] args)
        {
            Class1 c = new Class1();
            //step2 instantiating delegate
            AddDelegate ad = new AddDelegate(c.AddNums);

            // step 3 calling the delegate
            //ad(100, 50);
            ad.Invoke(100, 30);
            SayDelegate sd = new SayDelegate(Class1.SayHello);
            // string str = sd("Hello");
            string str = sd.Invoke("rahul");

            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #23
0
ファイル: Program.cs プロジェクト: Atulbabar9192/source
        static void Main(string[] args)
        {
            Program     P  = new Program();
            AddDelegate ad = new AddDelegate(P.AddNums); // non-static method not called directly hence called by instance//

            ad.Invoke(100, 500);                         //invoke is anothe method for calling//

            // P.AddNums(10, 20);

            SayDelegate sd  = new SayDelegate(SayHello);   //directly static member can access under static block//
            string      str = sd.Invoke("Atul babar");

            //string Str = Program.SayHello("Atul");


            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #24
0
        static void Main(string[] args)
        {
            Delegate p = new Delegate();
            //steep 2
            AddDelegate ad = new AddDelegate(p.Addnums);

            ad(120, 60);
            ad.Invoke(120, 60);

            SayDelegate sd  = new SayDelegate(SayHello);
            string      str = sd("Nitesh kus.mIYY.pIUHhyiaha");

            str = str.ToProper();
            string str1 = sd.Invoke("Nitesh Kushwaha");

            Console.WriteLine(str);
            Console.ReadKey();
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: rajkumarkore/Bizruntime
        static void Main(string[] args)
        {
            //here instatiate a class
            Program p = new Program();
            //p.Addsum(100, 20);

            AddDelegate ad = new AddDelegate(p.Addsum);

            //ad(100, 40); //local variable .

            ad.Invoke(100, 50);
            //here sayhello is static class so call that static class by name

            SayDelegate sd = new SayDelegate(Program.Sayhello);//static member dirctly call another staic block within the class.
            //string str = Program.Sayhello("Om sai");

            string str = sd.Invoke("sai");

            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #26
0
        static void Main()
        {
            Delegates p = new Delegates();

            p.AddNum(20, 30);
            string str = SayHello("Karren");//as SayHello is a static method,so calling by using the appropirate name of the class

            Console.WriteLine(str);

            //2) Instantiating the delegate
            Console.WriteLine();
            Console.WriteLine("Calling using the delegate");
            AddDelegate ad = new AddDelegate(p.AddNum); // It isnt a static class ,so we need to class the method by using the instance p
            SayDelegate sd = new SayDelegate(SayHello); // the method SayHello is static method

            ad(20, 30);
            ad.Invoke(10, 10); // or we can use it like this
            str = sd("Karren");
            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #27
0
        static void Main(string[] args)
        {
            Delegates p = new Delegates();
            //Step 2: Instantiating a delegate
            AddDelegate ad = new AddDelegate(p.AddNums);

            //Step 3: Calling a delegate
            ad.Invoke(100, 50);
            //ad(100, 50); -- Optional way to call delegate
            //Step 2: Instantiating a delegate
            SayDelegate sd = new SayDelegate(Delegates.SayHello);
            //Step 3: Calling a delegate
            string str = sd.Invoke("Manish");

            // string str = sd("Manish");

            //p.AddNums(100, 50);
            //string str = Delegates.SayHello("Manish");
            Console.WriteLine(str);
            Console.ReadLine();
        }
コード例 #28
0
        static void Main(string[] args)
        {
            #region Instantiating a delegate with methods
            SayDelegate del = new SayDelegate(SayHello);                            // The SayDelegate points to SayHello
            SayDelegate bye = new SayDelegate(SayBye);                              // The SayDelegate points to SayBye
            SayDelegate wow = new SayDelegate(x => Console.WriteLine($"Wow {x}!")); // The SayDelegate points to an anonymous method

            // Using the delegate ( Points to methods )
            del("Bob");
            bye("Jill");
            wow("Greg");
            #endregion

            #region Passing a method to a delegate parameter
            SayWhatever("Bob", SayHello);
            SayWhatever("Jill", SayBye);
            SayWhatever("Greg", x => Console.WriteLine($"Wow {x}!"));
            SayWhatever("Anne", x =>
            {
                Console.WriteLine($"Stuff with {x}");
                Console.WriteLine($"Other stuff with {x}");
            });
            #endregion
            #region Making a high order method
            Console.WriteLine("NUMBER MASTER! IT IS REALLY MASTER AT NUMBERS!");
            NumberMaster(2, 5, (x, y) => x + y);
            NumberMaster(2, 5, (x, y) => x - y);
            NumberMaster(2, 5, (x, y) => 0);
            NumberMaster(2, 5, (x, y) =>
            {
                if (x > y)
                {
                    return(x);
                }
                return(y);
            });
            #endregion
            Console.ReadLine();
        }
コード例 #29
0
        static void Main(string[] args)
        {
            SayDelegate del1 = new SayDelegate(SayHello);

            //Console.WriteLine(SayHello("Radmila"));
            Console.WriteLine(del1("Radmila"));

            //SayDelegate bye = new SayDelegate(SayBye);
            SayDelegate bye = SayBye;

            Console.WriteLine(bye("Radmila"));

            SayDelegate wow = new SayDelegate(x => $"Wow {x}!");

            Console.WriteLine(wow("Radmila"));


            ChatBotSays("Risto", SayHello);
            ChatBotSays("Risto", SayBye);
            //ChatBotSays("Risto", wow);
            ChatBotSays("Risto", x => $"Wow {x}!");
            ChatBotSays("Risto", x =>
            {
                return($"You have selected something ...");
            });

            Calculate(3, 5, (x, y) => x + y);
            Calculate(3, 5, (x, y) => x - y);
            Calculate(3, 5, (x, y) => x * y);
            Calculate(3, 5, (x, y) =>
            {
                if (y == 0)
                {
                    throw new DivideByZeroException();
                }

                return(x / y);
            });
        }
コード例 #30
0
 public static void SayHello(string name, SayDelegate sayDelegate)
 {
     Console.WriteLine(name + sayDelegate());
 }