zoukankan      html  css  js  c++  java
  • C# 委托简单示例

    C# 委托类似于C函数指针示例使用:

     1 using System;
     2 using System.Reflection;
     3 
     4 namespace ConsoleApp7
     5 {
     6     class Program
     7     {
     8         delegate void PrintHello();
     9 
    10         static void print1()
    11         {
    12             // 获取当前方法名
    13             Console.WriteLine(MethodBase.GetCurrentMethod().Name + "  run....");
    14         }
    15         static void print2()
    16         {
    17             Console.WriteLine(MethodBase.GetCurrentMethod().Name + "  run....");
    18         }
    19         static void print3()
    20         {
    21             Console.WriteLine(MethodBase.GetCurrentMethod().Name + "  run....");
    22         }
    23 
    24         static void Main(string[] args)
    25         {
    26             PrintHello test_print_del;
    27             test_print_del = print1;
    28             test_print_del += print2;
    29             test_print_del += print3;
    30 
    31             test_print_del();
    32             Console.WriteLine("=======移除方法=======");
    33             test_print_del -= print2;
    34             test_print_del();
    35 
    36             Console.ReadKey();
    37         }
    38     }
    39 }

    运行结果终端打印:

  • 相关阅读:
    JSP数据交互(一)
    response.setHeader()用法
    Vue初步认识
    Socket初步了解
    DOM4j的修改删除方式
    多线程
    ArrayList和Vector区别
    集合框架(一)
    深入C#数据类型
    深入.NET框架
  • 原文地址:https://www.cnblogs.com/chenxiaolinembed/p/15192477.html
Copyright © 2011-2022 走看看