• C#学习笔记(7)——委托


    说明(2017-5-29 22:22:50):

    1. 语法:public delegate void mydel();这一句在类外面,命名空间里面。

    2. 专门新建一个方法,参数是委托:

    public static void test(mydel mdl)
    {
      mdl();
    }

    3. 在main函数里,调用这个方法,参数是要使用的方法:

    test(show);

    4. 感觉test这个方法只是一个中转站,里面存放委托和参数。

    例1:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _04委托
     8 {
     9     public delegate void mydel();
    10     class Program
    11     {
    12        
    13         static void Main(string[] args)
    14         {
    15             test(show);
    16             Console.ReadKey();
    17         }
    18         public static void test(mydel mdl)
    19         {
    20             mdl();
    21         }
    22         public static void show()
    23         {
    24             Console.WriteLine("吃饭了!");
    25         }
    26     }
    27 }

     例2:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _04委托
     8 {
     9     public delegate int mydel(int x, int y);
    10     class Program
    11     {
    12 
    13         static void Main(string[] args)
    14         {
    15             test(add, 1, 2);
    16             Console.ReadKey();
    17         }
    18         public static void test(mydel mdl, int x, int y)
    19         {
    20             Console.WriteLine(mdl(x, y));
    21         }
    22         public static int add(int x, int y)
    23         {
    24             return x + y;
    25         }
    26     }
    27 }
  • 相关阅读:
    interview
    lvs简介
    编译参数说明
    nginx-arch
    network
    linux 密码修改方法
    ps命令详解
    sed 命令+正则表达式
    LINUX SCP 命令详解
    linux下mysql的root密码忘记解决方
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/6919224.html
Copyright © 2020-2023  润新知