C#方法可以返回一个委托吗? 如题..可以返回一个委托吗?或者类似的实现...像这样的,可以吗?public System.Deletegate Test(){ return "委托";} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然可以:Action<string> foo(int type){ if (type == 0) return x => Console.WriteLine(x); else return x => Console.WriteLine("type != 0" + x);}...foo(1)("Hello");foo(0)("Hello"); 可以的,方法的返回值可以是任意类型像:String Int 结构类型 类 委托等 static void HumanSay() { Console.WriteLine("Human say."); } static void AnimalSay() { Console.WriteLine("Animal say."); } static Delegate HowToSay(string animal) { switch(animal) { case "Human": return new Action(HumanSay); case "Animal": return new Action(AnimalSay); default: return null; } } static void Main(string[] args) { Delegate say = HowToSay("Animal"); say.DynamicInvoke(); return;返回Delegate类型不是好方法。会加重依赖返回一个具体的委托类型较好如Action HowToSay(string animal) 其实不用把委托看出很复杂,你就把它看成和整型一样,只不过整型变量里面你存放的是一个整型值,而在委托变量里面存放的是一个方法,当你在使用这个委托变量的时候,就相当于调用了这个方法。委托的好处主要在于给委托变量赋值的人和使用委托变量的人可以不是同一个。就好像要你设计Button类,在你设计的时候,你是无法知道使用这个按钮的人是要进行保存还是删除,或者取消的。怎么办呢,只能是你在Button类中创建一个委托ClickEventHandler类型的变量ClickEvent(也就是我们常说的事件),然后让用户为这个变量赋值(指定一个方法),我们称之为订阅事件。当用户点击按钮时,按钮就使用这个变量,调用了某个方法。这样作为按钮,虽然我不知道要做什么,但是我还是可以做。如果你明白了我说的,那么返回一个委托和返回一个int是一样的。只不过委托类型需要自己定义,而int是系统已经定义好的。现在我们简单分析对一个int变量的使用,如int i;i = 3;i++;在上述代码中包含以下步骤:1、创建int类(系统完成);2、定义int变量 i3、给变量赋值 4、使用变量委托类型的使用和上面一样,需要1、创建委托类(你完成);2、定义委托变量3、给委托变量赋值 4、使用委托变量(即调用方法)委托类型的定义和要调用的方法签名必须一致,所以一般情况下,我会先确定要实现的方法的签名下面我要实现的示例是:学生将考试成绩拿回家给父亲,等待父亲处理。//0、创建父亲处理成绩的方法class Father{ //0.定义要执行的方法 public void LookScore(int score) { string msg = "I 服了 u!"; if(score > 90) msg = "very good!"; else if(score > 60) msg = "just so so"; Console.WriteLine(msg); }}//1、定义委托类型MyDelegateName,类型是大家都要用的,一般是范围越广越好,所以放在类的外面定义public delegate void MyDelegateName(int s);class Student{ //2、声明委托变量,变量属于类 public MyDelegateName FinishTest; //4、使用变量 public void GetScore() { Console.WriteLine("公布成绩了!"); int socre = 95; if(FinishTest != null) FinishTest(socre); //使用委托变量 }}class Program{ Father f = new Father(); Student s = new Student(); Console.WriteLine("孩子开始上学了,需要关心他的成绩,考完试就看。没有考试的时候就别找我,忙!"); s.FinishTest = new MyDelegateName(f.LookScore);//给委托变量赋值,注意赋值的是father,而Student是不知道father会怎么做的。 s.GetScore();} C#中继承的一个小问题 可以在dos中输出output.txt文件吗,而不是字符串,格式是否会变化,多谢! sapi线程同步问题 C#通过一个窗口的事件改变另一个窗口的背景色 实体IList 如何转换成IList<object> 自动向客户机子向注册表里注册外接程序 类似DropDownList的控件 急!~!~!在线等!~(简单问题) 请教高手: 用C#如何实现多人聊天程序?并且有声音提示.在线等待中.......... ---oracle存储过程的调用问题--- 控制多个摄像头 TreeView的Check功能,向高手请教一个问题
{
if (type == 0)
return x => Console.WriteLine(x);
else
return x => Console.WriteLine("type != 0" + x);
}...foo(1)("Hello");
foo(0)("Hello");
static void HumanSay() {
Console.WriteLine("Human say.");
} static void AnimalSay() {
Console.WriteLine("Animal say.");
} static Delegate HowToSay(string animal) {
switch(animal) {
case "Human":
return new Action(HumanSay);
case "Animal":
return new Action(AnimalSay);
default:
return null;
}
} static void Main(string[] args) {
Delegate say = HowToSay("Animal");
say.DynamicInvoke();
return;返回Delegate类型不是好方法。会加重依赖
返回一个具体的委托类型较好
如
Action HowToSay(string animal)
int i;
i = 3;
i++;
在上述代码中包含以下步骤:
1、创建int类(系统完成);
2、定义int变量 i
3、给变量赋值
4、使用变量
委托类型的使用和上面一样,需要
1、创建委托类(你完成);
2、定义委托变量
3、给委托变量赋值
4、使用委托变量(即调用方法)委托类型的定义和要调用的方法签名必须一致,所以一般情况下,我会先确定要实现的方法的签名
下面我要实现的示例是:学生将考试成绩拿回家给父亲,等待父亲处理。//0、创建父亲处理成绩的方法
class Father
{
//0.定义要执行的方法
public void LookScore(int score)
{
string msg = "I 服了 u!";
if(score > 90)
msg = "very good!";
else if(score > 60)
msg = "just so so";
Console.WriteLine(msg);
}
}//1、定义委托类型MyDelegateName,类型是大家都要用的,一般是范围越广越好,所以放在类的外面定义
public delegate void MyDelegateName(int s);class Student
{
//2、声明委托变量,变量属于类
public MyDelegateName FinishTest; //4、使用变量
public void GetScore()
{
Console.WriteLine("公布成绩了!");
int socre = 95; if(FinishTest != null)
FinishTest(socre); //使用委托变量
}
}class Program
{
Father f = new Father();
Student s = new Student();
Console.WriteLine("孩子开始上学了,需要关心他的成绩,考完试就看。没有考试的时候就别找我,忙!");
s.FinishTest = new MyDelegateName(f.LookScore);//给委托变量赋值,注意赋值的是father,而Student是不知道father会怎么做的。
s.GetScore();
}