zoukankan      html  css  js  c++  java
  • C#委托举例

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _000控制台练习
     8 {
     9     //声明委托,写在类外面,命名空间内
    10     public delegate void delSay(string name);
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //调用这个函数,参数为“名字”和“委托”,就相当于“委托”来操作“名字”
    16             //委托的实质就是,把函数当作函数的参数
    17             test("jack", sayChinese);
    18             //也可以把两种打招呼方式赋值给一个“委托”,如果用+=,就是再绑定一个方法
    19             //将输出两种打招呼方法
    20             delSay delsay1 = sayEnglish;
    21             delsay1 += sayChinese;
    22             //或者也可以直接用“委托”操作“姓名”
    23             delsay1("Ashley");
    24 
    25             Console.ReadKey();
    26         }
    27         //写一个函数,参数是“名字”和“委托”
    28         public static void test(string name, delSay delsay)
    29         {
    30             //函数里面是“委托”操作“名字”
    31             delsay(name);
    32         }
    33         //打招呼,使用委托后就用不到了
    34         public static void sayHello(string name)
    35         {
    36             sayEnglish(name);
    37         }
    38         //英语打招呼
    39         public static void sayEnglish(string name)
    40         {
    41             Console.WriteLine("Hello," + name);
    42         }
    43         //汉语打招呼
    44         public static void sayChinese(string name)
    45         {
    46             Console.WriteLine("你好," + name);
    47         }
    48     }
    49 }
  • 相关阅读:
    ADO.NET 根据实体类自动生成添加修改语句仅限Oracle使用
    C# 实体对象作为参数统一去除空格
    jQuery 前端复选框 全选 反选 下拉菜单联动
    C# 后台服务器端 Get 请求函数封装
    服务器404错误页面
    vue 封装公用函数
    Vue 生命周期
    Oracle 查看表结构
    ubuntu源配置
    外观(Facade)模式
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/5515960.html
Copyright © 2011-2022 走看看