Beispiel #1
0
        static void Main(string[] args)
        {
            CalcDelegate calculator = Calc.Add;

            Console.WriteLine($"{calculator.Method.Name} : {calculator?.Invoke(2, 4)}");
            calculator += Calc.Mult;
            Console.WriteLine($"{calculator.Method.Name} : {calculator?.Invoke(2, 4)}");
            Console.WriteLine("========================");
            foreach (Delegate deleg in calculator.GetInvocationList())
            {
                Console.WriteLine($"{deleg.Method.Name} : {deleg.DynamicInvoke(5,5)}");
            }
        }
Beispiel #2
0
        public HalloDelegate()
        {
            EinfacherDelegate meinDele              = EinfacheMethode;
            Action            meinDeleAlsAction     = EinfacheMethode;
            Action            meinDeleAlsActionAno  = delegate() { Console.WriteLine("Hallo Ano"); };
            Action            meinDeleAlsActionAno2 = () => { Console.WriteLine("Hallo"); };
            Action            meinDeleAlsActionAno3 = () => Console.WriteLine("Hallo");

            DelegateMitPara meinDelemitPara          = MethodeMitPara;
            Action <string> meinDelemitParaAlsAction = MethodeMitPara;
            DelegateMitPara meinDelemitParaAno       = (string name) => { Console.WriteLine($"Hallo {name}"); };
            Action <string> meinDelemitParaAno2      = (name) => Console.WriteLine($"Hallo {name}");
            Action <string> meinDelemitParaAno3      = x => Console.WriteLine($"Hallo {x}");

            CalcDelegate          calcDele     = Minus;
            Func <int, int, long> calcDeleFunc = Sum;
            CalcDelegate          calcDeleAno  = (int x, int y) => { return(x + y); };
            CalcDelegate          calcDeleAno2 = (x, y) => x + y;

            List <string> texten = new List <string>();

            texten.Where(x => x.StartsWith("b"));
            texten.Where(Filter);

            long re     = calcDeleFunc.Invoke(6, 78);
            long result = calcDele.Invoke(56, 777);
        }
        public HalloDelegates()
        {
            EinfacherDelegate meinDele;

            meinDele = EinfacheMethode;
            meinDele.Invoke();
            Action myAction     = EinfacheMethode;
            Action myActionAno  = delegate() { Console.WriteLine("AALT"); }; //bis .net 2.0
            Action myActionAno2 = () => { Console.WriteLine("Lambda"); };    //ab .net 3
            Action myActionAno3 = () => Console.WriteLine("Lambda");

            DelegateMitParameter deleMitPara       = MethodeMitParameter;
            Action <string>      actionMitPara     = MethodeMitParameter;
            Action <string>      actionMitParaAno  = (string name) => { Console.WriteLine("Hallo " + name); };
            Action <string>      actionMitParaAno2 = (name) => Console.WriteLine("Hallo " + name);
            Action <string>      actionMitParaAno3 = x => Console.WriteLine("Hallo " + x); //nur bei 1 parameter

            CalcDelegate          calcDele   = Minus;
            long                  result     = calcDele.Invoke(45, 67);
            Func <int, int, long> calcAsFunc = Sum;

            Func <int, int, long> calcAsFuncAno  = (int a, int b) => { return(a + b); };
            Func <int, int, long> calcAsFuncAno2 = (a, b) => { return(a + b); };
            Func <int, int, long> calcAsFuncAno3 = (a, b) => a + b;

            List <string> texte   = new List <string>();
            var           resultB = texte.Where(x => x.StartsWith("b")); //<--

            var resultB2 = texte.Where(Filter);
        }
        public HalloDelegate()
        {
            EinfacheDelegate meineDelegate         = EinfacheMethode;
            Action           meinDeleAlsAction     = EinfacheMethode;
            Action           meinDeleAlsActionAno  = delegate() { Console.WriteLine("Hallooo"); };
            Action           meinDeleAlsActionAno2 = () => { Console.WriteLine("Hallooo"); };
            Action           meinDeleAlsActionAno3 = () => Console.WriteLine("Hallooo");

            DelegateMitParameter meinDeleMitPara           = MethodeMitPara;
            Action <string>      meinDeletMitParaAlsAction = MethodeMitPara;
            Action <string>      meinDeleMitParaAno        = (string txt) => { Console.WriteLine(txt); };
            Action <string>      meinDeleMitParaAno2       = (txt) => Console.WriteLine(txt);
            Action <string>      meinDeleMitParaAno3       = x => Console.WriteLine(x);

            CalcDelegate          calc        = Minus;
            Func <int, int, long> calcAlsFunc = Sum;
            CalcDelegate          calcAno     = (int a, int b) => { return(a + b); };
            CalcDelegate          calcAno2    = (a, b) => { return(a + b); };
            CalcDelegate          calcAno3    = (a, b) => a + b;

            long result = calc.Invoke(3, 4);

            List <string> texte = new List <string>();
            List <string> nurB  = texte.Where(x => x.StartsWith("b")).ToList();
            List <string> nurB2 = texte.Where(Filter).ToList();

            foreach (var item in texte)
            {
                //   if(item.StartsWith("b"))
                //...
            }
        }
Beispiel #5
0
        public HalloDelegate()
        {
            EinfacheDelegate meineDele              = EinfacheMethode;
            Action           meineDeleAlsAction     = EinfacheMethode;
            Action           meineDeleAlsActionAno  = delegate() { Console.WriteLine("Ich habe keinen Namen"); };
            Action           meineDeleAlsActionAno2 = () => { Console.WriteLine("Ich habe keinen Namen"); };
            Action           meineDeleAlsActionAno3 = () => Console.WriteLine("Ich habe keinen Namen");


            DelegateMitPara deleMitPara     = MethodePara;
            Action <string> paraAlsAction   = MethodePara;
            DelegateMitPara deleMitParaAno  = (string txt) => { Console.WriteLine(txt); };
            Action <string> deleMitParaAno2 = (txt) => Console.WriteLine(txt);
            DelegateMitPara deleMitParaAno3 = x => Console.WriteLine(x);

            CalcDelegate          calcDele     = Minus;
            Func <int, int, long> calcFunc     = Sum;
            CalcDelegate          calcDeleAno  = (int x, int y) => { return(x + y); };
            CalcDelegate          calcDeleAno2 = (x, y) => { return(x + y); };
            CalcDelegate          calcDeleAno3 = (x, y) => x + y;

            List <string> texte = new List <string>();

            texte.Where(x => x.StartsWith("b"));
            texte.Where(Filter);

            long res = calcDele.Invoke(56, 23);
        }
        public HalloDelegate()
        {
            EinfacherDelegate meinDele        = EinfacheMethode;
            Action            meineAction     = EinfacheMethode;
            Action            meineActionAno  = delegate() { Console.WriteLine("Halölo"); };
            Action            meineActionAno2 = () => { Console.WriteLine("Hallo"); };
            Action            meineActionAno3 = () => Console.WriteLine("Hallo");

            DelegateMitPara deleMitPara       = MethodeMitPara;
            Action <string> deleMitParaAction = MethodeMitPara;
            DelegateMitPara deleMitParaAno    = (string msg) => { Console.WriteLine(msg); };
            DelegateMitPara deleMitParaAno2   = (msg) => Console.WriteLine(msg);
            DelegateMitPara deleMitParaAno3   = x => Console.WriteLine(x);

            CalcDelegate          calc     = Multi;
            Func <int, int, long> calcFunc = Sum;
            CalcDelegate          calcAno  = (x, y) => { return(x + y); };
            CalcDelegate          calcAno2 = (x, y) => x + y;

            var text = new List <string>();

            text.Where(x => x.StartsWith("b"));
            text.Where(Filter);

            long res = calc.Invoke(1, 5);
        }
Beispiel #7
0
        delegate int CalcDelegate(int a, int b);  // 델리게이트 만듬.두개의 정수를 받아들이고, 1개의 정수를 리턴한다는 함수를 이렇게 정의했다.

        // c는 메서드 원형 전체의 이름이다.
        void Calc2(int a, int b, CalcDelegate calc) // 모든 메서드를 받는게 아니라 어떠한 메서드 원형을 갖는 메서드를 가질까를 정해야 한다.
        {
            int res = calc.Invoke(a, b);            // Invoke를 사용할 수 있는 이유는 이것이 delegate로부터 상속된 메소드이기 때문이다.

            res = calc(a, b);                       // 이렇게도 사용할 수 있다.
                                                    // 시간이 더 있었으면 재밌는걸 만들 수 있을거 같은데.. 시간이 모자란게 한이다.
            Console.WriteLine("사용함수 : {0}", calc.Method);
            Console.WriteLine($"f({a},{b}) = {res}");
        }