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 }
  • 相关阅读:
    set 用法、小结
    AC 自动机优化
    HDU 2222 Keywords Search 【ac自动机】
    组合数学 隔板法
    BZOJ1303_中位数图_KEY
    初识Trie_对Trie的一些认识
    网络流Edmonds-Karp算法入门
    Codevs1332_上白泽慧音_KEY
    Fliptil_KEY
    2017Noip普及组游记
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/6919224.html
Copyright © 2011-2022 走看看