123456789101112131415161718192021222324252627282930313233 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Remoting.Messaging;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleDelegate
- {
- class Test
- {
- public int Add(int x, int y)
- {
- return x + y;
- }
- }
- internal class Program
- {
- //定义委托
- public delegate int DAdd(int x, int y);
- static void Main(string[] args)
- {
- Test test = new Test();
- //将委托与Add方法相关联
- DAdd d = test.Add;
- //再想调用Test类中的Add方法时,直接调用委托就可以了
- int num = d(2,3);
- Console.WriteLine(num);
- Console.ReadLine();
- }
- }
- }
|