using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateSummary {
public delegate void GetIntDelegate(int a); //声明一个委托
public class getIntClass {
public static void SetDelegateString(int a,GetIntDelegate getIntDelegate) {//使用委托
getIntDelegate(a);
}
public void getInt1(int a) { //方法1
Console.WriteLine("getInt1方法调用,参数为:" + a);
}
public void getInt2(int a) { //方法2
Console.WriteLine("getInt2方法调用,参数为:" + a);
}
}
class Program {
static void Main(string[] args) {
getIntClass gc=new getIntClass();
getIntClass.SetDelegateString(5, gc.getInt1); //方法1,2作为委托的参数
getIntClass.SetDelegateString(10, gc.getInt2);
Console.WriteLine("=====================");
GetIntDelegate getIntDelegate;
getIntDelegate = gc.getInt1; //将方法1,2绑定到委托
getIntDelegate += gc.getInt2;
getIntClass.SetDelegateString(100, getIntDelegate);
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateSummary {
class Program {
static void Main(string[] args) {
TestAction<string>(getString, "WhiteTaken"); //传入方法
TestAction<int>(getInt, 666);
TestAction<int, string>(getStringAndInt, 666, "WhiteTaken");
Console.Read();
}
public static void TestAction<T>(Action<T> action,T p1) { //Action传入一个参数测试
action(p1);
}
public static void TestAction<T, P>(Action<T, P> action, T p1, P p2) { //Action传入两个参数测试
action(p1,p2);
}
public static void getString(string a) { //实现int类型打印
Console.WriteLine("测试Action,传入string,并且传入的参数为:" +a);
}
public static void getInt(int a) { //实现String类型打印
Console.WriteLine("测试Action,传入int,并且传入的参数为:" + a);
}
public static void getStringAndInt(int a, string name) { //实现int+string类型打印
Console.WriteLine("测试Action,传入两参数,并且传入的参数为:" + a+":"+name);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有