zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    使用vs2010编译 Python SIP PyQt4
    谷歌编程指南
    【转】微策略面经相关资料
    KMP 算法
    C++ 拷贝构造函数
    虚继承 虚表 定义一个不能被继承的类
    cache的工作原理
    背包问题
    【转】C/C++ 内存对齐
    【转】 Linux/Unix 进程间通信的各种方式及其比较
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/6919224.html
Copyright © 2011-2022 走看看