c#的委托(2)之调用实例化方法

来源:互联网 发布:淘宝店运营计划书 编辑:程序博客网 时间:2024/04/30 08:26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delegate
{
    //声明一个委托关键字为 delegate
    delegate int NumberChanger(int n);

    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            NumberChanger nc2 = new NumberChanger(mc.AddNum);
            nc2(35);

            Console.WriteLine("Value of instance num:{0}", mc.num);
            Console.ReadLine();
        }   

    }

    class MyClass {
        public int num = 10;
        public int AddNum(int p) 
        {
            num += p;
            return num;
        }
    }
}

0 0
原创粉丝点击