匿名委托方法如何当作参数来传递
匿名委托方法如何当作参数来传递private static void Passit(string example)
{
example = "king";
}
static void Main(string[] args)
{
string firstName = "aaa";
string lastName = "bbb";
Passit(delegate(string firstname)
{
Console.WriteLine("come on");
Console.WriteLine(firstname + "<>");
});
}
怎么不行啊。。要怎么做啊 --------------------编程问答-------------------- Passit接受的是个字符串参数,不是代理
using System.Collections.Generic;
private static void Passit(Action<string> a)
{
a("King");
}
static void Main(string[] args)
{
string firstName = "aaa";
string lastName = "bbb";
Passit(delegate(string firstname)
{
Console.WriteLine("come on");
Console.WriteLine(firstname + " < >");
});
}
--------------------编程问答-------------------- 为什么要用泛型。。
那参数是普通的类呢。。。也需要这样? --------------------编程问答-------------------- 那么就要这么做:
delegate void MyDelegateTime(string xxx);
private static void Passit(MyDelegateTime a)
{ --------------------编程问答-------------------- delegate void MyDelegateTime(string xxx);
private static void Passit(MyDelegateTime a)
{ }
Passit((MyDelegateTime)delegate(string s)
{
}
是这样吗?要强制转换吧 --------------------编程问答-------------------- 不需要转换的 --------------------编程问答-------------------- private static void Passit(Action <string > a,Action <string > b)
{
a("King");
b("dd");
}
static void Main(string[] args)
{
string firstName = "aaa";
string lastName = "bbb";
Passit(delegate(string firstname,string lastname)
{
Console.WriteLine("come on");
Console.WriteLine(firstname + " < >"+lastname);
});
}
两个参数是这样吗?好像不行啊
指导一下 --------------------编程问答-------------------- 看清楚了,你的匿名代理是2个string参数
delegate(string firstname,string lastname)
试这个
delegate void MyDelegateTime(string a, string b);
private static void Passit(MyDelegateTime aa)
{
aa("King","dd");
} --------------------编程问答-------------------- 思归把Linq Lambda表达式思想加进去了,反而不好理解了,o(∩_∩)o... --------------------编程问答-------------------- 你好强,你好强
补充:.NET技术 , C#