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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ExDelegate
    {
        delegate void PrintFunction();
        delegate void MyDel(ref int x);
        class Test
        {
            public void print1()
            {
                Console.WriteLine("Print1---instance");
            }
            public static void print2()
            {
                Console.WriteLine("Print2---static");
            }
        }
        class MyClass
        {
            public void Add2(ref int x)
            {
                x += 2;
            }
            public void Add3(ref int x)
            {
                x += 3;
            }
            static void Main(string[] args)
            {
                MyClass mc = new MyClass();
                MyDel md = mc.Add2;
                md += mc.Add3;
                int x = 5;
                md(ref x);
                Console.WriteLine("Value:{0}",x);
                Test t1 = new Test();
                PrintFunction pf;
                pf = t1.print1;
                pf += Test.print2;
                if (null != pf)
                {
                    pf();
                }
                else
                {
                    Console.WriteLine("Delegate is empty");
                }
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    1075: 聚餐人数统计
    1074: 百钱买百鸡
    1072: 青蛙爬井
    1073: 级数求和
    1071: 分解质因子
    1070: 小汽车的位置
    1068: 二进制数
    2019 牛客多校 第六场
    2019 牛客多校 第五场
    2019 牛客多校 第二场
  • 原文地址:https://www.cnblogs.com/sulong/p/4797756.html
Copyright © 2011-2022 走看看