C#中的委托(delegate)
发布日期:2021-05-10 10:50:48 浏览次数:12 分类:精选文章

本文共 3423 字,大约阅读时间需要 11 分钟。

C#������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������

������������������������������������������������������������C#������������������������������������������������������������������������������������������������������������������������������������������������������delegate���������������������������������������������������������

���������������������

  • ������������������

    ������delegate������������������������������������������������

    public delegate void AddDelete(int a, int b);

    ������������������������AddDelete������������������������������������������������������������������

  • ������������

    ���������������������������������������������������������������������������������������������

    public static void Add(int a, int b) {    Console.WriteLine(a + b);}

    ������������������������������AddDelete������������

  • ������������������

    ������new���������������������������������������������������������������������������

    AddDelete adddelete = new AddDelete(Add);

    ������������������������������������Add���������������AddDelete������������

  • ������������

    ������������������������������������������������������������

    adddelete(1, 2);

    ������������Add(1, 2)���������������3���

  • ������������������

    ���������������������������������������������������������������������������������������������������������������������������������������

    AddDelete adddelete = delegate(int a, int b) { Console.WriteLine(a + b); };adddelete(1, 2);

    ������������������������������������������������������������������������������������������

    ������������������

    ������������������������������������������������������������������������������������������������������������������

    public delegate int MyDel(int name);static int Add1(int a) {    var b = 10 + a;    Console.WriteLine(b);    return b;}static int Add2(int a) {    var b = 10 - a;    Console.WriteLine(b);    return b;}static void Main(string[] args) {    MyDel add = new MyDel(Add1);    add += new MyDel(Add2);    var result = add(10);    Console.WriteLine(result);}

    ������+=���������������������������������������������������������������������������������������+=���������������������������������������������������������������������������������������������

    ������������������������

    Q������������������������������������

    ������������������������������������������������

    class MyClass {    public void Add(int a, int b) {        Console.WriteLine(a + b);    }}static void Main(string[] args) {    MyClass myClass = new MyClass();    MyDel add = new MyDel(myClass.Add);    add(1, 2);}

    ���������������������Add���������������������������������������������myClass���������������

    Q������������������������������������void���

    ��������������������������������������������������������������������� tribes ��������������������������������������������������������������������� C# ���������������������������������������������������������������������������������������������������

    ������

    ���������C#������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:winform 输出日志框信息
    下一篇:Setup Factory 安装程序的图标

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月28日 22时44分49秒