zoukankan      html  css  js  c++  java
  • C# delegate

    1. delegate

    example1

    class Program
    {
            public delegate int MyDelegate(int i);
    
            int MyFunc(int i)
            {
                return i;
            }
    
            public void Foo()
            {
                MyDelegate f = MyFunc;
          
                Console.WriteLine(f(2));
                Console.ReadKey();
            }
    }

    example 2

    // declare the delegate type
        public delegate string MyDelegate(int arg1, int arg2);
    
    
        class Program
        {
            // INSERT DELEGATES HERE
            static string func1(int a, int b)
            {
                return (a + b).ToString();
            }
            static string func2(int a, int b)
            {
                return (a * b).ToString();
            }
    
            static void Main(string[] args)
            {
                MyDelegate f = func1;
                Console.WriteLine("The number is: " + f(10, 20));
                f = func2;
                Console.WriteLine("The number is: " + f(10, 20));
    
                Console.WriteLine("
    Press Enter Key to Continue...");
                Console.ReadLine();
            }
        }

     2. anonymous delegate

     public delegate string MyDelegate(int arg1, int arg2);
    
        class Program
        {
            static void Main(string[] args)
            {
                // SNIPPET GOES HERE
                MyDelegate f = delegate(int arg1, int arg2)
                {
                    return (arg1 + arg2).ToString();
                };
                Console.WriteLine("The number is: " + f(10, 20));
                // Keep the console window open until a key is pressed
                Console.WriteLine("
    Press Enter Key to Continue...");
                Console.ReadLine();
            }
        }

     3. composable delegate (call multiple delegates with one function call)

     // declare the delegate type
        public delegate void MyDelegate(int arg1, int arg2);
    
        class Program
        {
            static void func1(int arg1, int arg2)
            {
                string result = (arg1 + arg2).ToString();
                Console.WriteLine("The number is: " + result);
            }
            static void func2(int arg1, int arg2)
            {
                string result = (arg1 * arg2).ToString();
                Console.WriteLine("The number is: " + result);
            }
            static void Main(string[] args)
            {
                MyDelegate f1 = func1;
                MyDelegate f2 = func2;
                MyDelegate f1f2 = f1 + f2;
    
                // call each delegate and then the chain
                Console.WriteLine("Calling the first delegate");
                f1(10, 20);
                Console.WriteLine("Calling the second delegate");
                f2(10, 20);
                Console.WriteLine("
    Calling the chained delegates");
                f1f2(10, 20);
    
                // subtract off one of the delegates
                Console.WriteLine("
    Calling the unchained delegates");
                f1f2 -= f1;
                f1f2(20, 20);
    
    
                Console.WriteLine("
    Press Enter Key to Continue...");
                Console.ReadLine();
            }
        }
  • 相关阅读:
    MSIL指令速查表
    .NET中的内存管理,GC机制,内存释放过程,各种内存释放方法
    程序员学专业英语
    C#入门不简单(上网找beginning c# objects时看到的一些话,很有同感)
    关掉在打开的做法很有必要
    视图刷新
    创建 平面求交点
    3dmas 的硬显示 这个就是其中既各个 最后两个是 对应显示操作的
    这句话是我在渲染 程序贴图用到的代码
    设置到基础层
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/5147097.html
Copyright © 2011-2022 走看看