• C# 代理做为函数参数的时候


    View Code
     1 using System;
     2 
     3 // 声名一个代理,与类同级(其本质就是类)
     4 delegate void theHandler(int i);
     5 
     6 class TestClass
     7 {
     8     public void plus(int i) {
     9         Console.WriteLine( " i + i = " + (i + i).ToString());
    10     }
    11     // 这个方法中的第一个参数 是一个代理.
    12     // 我们可以直接传入一个与其签名相同的 方法名.
    13     public void delegateMethod(theHandler del,int i) {
    14         del(i);
    15     }
    16     
    17     
    18 }
    19 
    20 
    21 class Program
    22 {
    23     // 事件声名要在方法体的外部,其本质为代理的实例
    24     static event theHandler thEvent;
    25     
    26     static void Main(string[] args)
    27     {
    28         if (args.Length == 1) {
    29             TestClass tc = new TestClass();
    30             // 直接装 签名相同的方法 传到 这个代理的位置
    31             tc.delegateMethod(tc.plus,int.Parse(args[0]));
    32             // 使用事件(代理的实例)
    33             // 直接实例化代理, 必须传入一个同签名方法,不然编译出错
    34             // 而声名一个事件时可以不传同签名方法.
    35             //theHandler th = new theHandler();
    36             // 将同签名的方法直接附值给 事件.
    37             thEvent = tc.plus;
    38             tc.delegateMethod(thEvent,int.Parse(args[0]));
    39         } else {
    40             Console.WriteLine("Please input only ONE integer Number.");
    41         }
    42     }
    43 }
  • 相关阅读:
    KY2成绩排序
    python 获取list中元素的索引
    pandas 读取指定一列数据
    python 删除列表中的第一位元素
    python 时间戳
    python 除法保留小数点后两位
    python 读取excel表格的一列数据并去重
    python中获取Excel表格sheet页整页内容
    IDEA创建spring boot项目
    servlet一些问题
  • 原文地址:https://www.cnblogs.com/easyfrog/p/2484949.html
Copyright © 2020-2023  润新知