• C#中的委托


      自学的过程中,一直没跨过委托这个槛儿,今天刚好有点空,拿起书对照着MSDN再看了一遍。简要记录一二,以供后续温习之便。

      MSDN官方原文:委托是一种定义方法签名(由返回类型和参数组成)的类型,可以与具有兼容签名的任何方法关联。 可以通过委托调用方法。委托用于将方法作为参数传递给其他方法。

    委托入门

      使用委托的规则如下:首先使用 delegate 关键字声明一个委托类型,指定其返回类型和参数。通过 new 操作符,可以使用委托声明来创建一个委托类型的实例。在创建委托类型的实例时,目标方法必须与委托声明的签名精确匹配。

      下面先给出一个数组冒泡排序的示例代码,后面对此进行改造以讲解如何使用委托。

    namespace Hans_GIS.DelegateExample01
    {
        class Program
        {
            static void Main(string[] args)
            {
                int temp;
                int[] array  = new int[]{ 23, 28, 24, 26, 25 };
    
                for (int i = 0; i < array.Length - 1; i++)
                {
                    for (int j = i + 1; j < array.Length; j++)
                    {
                        if (Greater(array[i], array[j]))
                        {
                            temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                    }
                }
                
                foreach (int i in array)
                {
                    Console.Write("{0}  ", i);
                }
                Console.ReadKey();
            }
    
            static bool Greater(int iNum1, int iNum2)
            {
                if (iNum1 > iNum2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

      对上述代码略加改造,声明一个委托,创建委托实例,为之分配比较数值大小的方法,就可以调用委托了。如下所示:

    namespace Hans_GIS.DelegateExample01
    {
        delegate bool Compare(int i1,int i2);
    
        class Program
        {
            static void Main(string[] args)
            {
                Compare com = new Compare(Greater);
    
                int temp;
                int[] array  = new int[]{ 23, 28, 24, 26, 25 };
    
                for (int i = 0; i < array.Length - 1; i++)
                {
                    for (int j = i + 1; j < array.Length; j++)
                    {
                        if (com(array[i], array[j]))
                        {
                            temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                    }
                }
                
                foreach (int i in array)
                {
                    Console.Write("{0}  ", i);
                }
                Console.ReadKey();
            }
    
            static bool Greater(int iNum1, int iNum2)
            {
                if (iNum1 > iNum2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    

      每个委托都有自己的签名,例如上面的示例中委托声明:delegate bool Compare(int i1,int i2),这里所说的签名就是指该委托有两个int类型的参数,还有一个bool类型的返回值。在实例化委托时,需要为其分配一个方法,将其作为其构造函数的参数,如 Compare com = new Compare(Greater),在这里,Compare委托的实例com引用了Greater方法,调用com的时也就会调用Greater方法了。值得注意的是,该方法必须与委托声明具有同样的签名。如上例中的static bool Greater(int iNum1, int iNum2)。

    多路广播委托

      调用委托时,它可以调用多个方法,这称之为多路广播委托。可以使用加法运算符或加法赋值运算符(“+”或“+=”)向委托的方法列表(调用列表)中添加额外的方法;可以使用减法运算符或减法赋值运算符(“-”或“-=”)从调用列表中移除方法。

    namespace Hans_GIS.DelegateExample02
    {
        class Program
        {
            delegate void UppercaseDelegate(string input);
    
            static void UppercaseFirst(string input)
            {
                char[] buffer = input.ToCharArray();
                buffer[0] = char.ToUpper(buffer[0]);
                WriteOutput(input, new string(buffer));
            }
    
            static void UppercaseLast(string input)
            {
                char[] buffer = input.ToCharArray();
                buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
                WriteOutput(input, new string(buffer));
            }
    
            static void UppercaseAll(string input)
            {
                WriteOutput(input, input.ToUpper());
            }
    
            static void WriteOutput(string input, string strRe)
            {
                Console.WriteLine("Your string before: {0}", input);
                Console.WriteLine("Your string after: {0}", strRe);
            }
    
            static void Main()
            {
                UppercaseDelegate uppercase = new UppercaseDelegate(UppercaseFirst);
                uppercase += new UppercaseDelegate(UppercaseLast);
                uppercase += UppercaseAll;
                uppercase("hans_gis");
                Console.ReadKey();
            }
        }
    }
    

      结果输出:

    委托的异步回调

      既然委托实例是一个对象,那么就可以将其作为参数进行传递,也可以将其赋值给属性。如此,方法便可以接受一个委托参数,并且之后可以调用该委托,这称之为异步回调。

      对上面多路广播委托的代码做了一点改动,如下:

    namespace Hans_GIS.DelegateExample03
    {
        class Program
        {
            delegate string UppercaseDelegate(string input);
    
            static string UppercaseFirst(string input)
            {
                char[] buffer = input.ToCharArray();
                buffer[0] = char.ToUpper(buffer[0]);
                return new string(buffer);
            }
    
            static string UppercaseLast(string input)
            {
                char[] buffer = input.ToCharArray();
                buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
                return new string(buffer);
            }
    
            static string UppercaseAll(string input)
            {
                return input.ToUpper();
            }
    
            static void WriteOutput(string input, UppercaseDelegate del)
            {
                Console.WriteLine("Your string before: {0}", input);
                Console.WriteLine("Your string after: {0}", del(input));
            }
    
            static void Main()
            {
                WriteOutput("hans_gis", new UppercaseDelegate(UppercaseFirst));
                WriteOutput("hans_gis", new UppercaseDelegate(UppercaseLast));
                WriteOutput("hans_gis", new UppercaseDelegate(UppercaseAll));
                Console.ReadKey();
            }
        }
    }
    

      输出结果同上。

  • 相关阅读:
    notepad++一次去掉所有空行,然后加上2个空行
    mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错
    Xshell 5的快捷键
    word2010文档如何隐藏右侧灰色空白不可编辑区域
    记一次惊险的系统和分区修复
    nohup command 2>&1 & 的含义
    alexkn android第一行代码-7.广播
    android第一行代码-6.自定义控件的实现
    android第一行代码-5.监听器的两种用法和context
    android第一行代码-3.activity之间的调用跟数据传递
  • 原文地址:https://www.cnblogs.com/hans_gis/p/2208051.html
Copyright © 2020-2023  润新知