func lambda Action 匿名委托
2011-01-26 15:07 JingXinSoft 阅读(194) 评论(0) 收藏 举报直接上代码:
 代码
代码
public delegate void ActionMethodHandler(int p);
class FuncTest
{
public static Action<int> myAction;
public static string TestMethod(int msg)
{
return "test:" + msg.ToString();
}
public static void CallMethod<T>(Func<T,string> methodName,T item)
{
Console.WriteLine(methodName(item));
}
public static void ActionMethod(int p)
{
Console.WriteLine(p.ToString());
}
}
 代码
代码
1 static void Main(string[] args)
2 {
3 FuncTest.CallMethod(FuncTest.TestMethod,3);
4
5
6 FuncTest.CallMethod<int>(FuncTest.TestMethod, 3);
7
8 FuncTest.myAction = FuncTest.ActionMethod;
9 FuncTest.myAction(3);
10
11 ActionMethodHandler actionMethod = new ActionMethodHandler(FuncTest.ActionMethod);
12 actionMethod(3);
13
14 ActionMethodHandler actionMethod2 = new ActionMethodHandler(p => { Console.WriteLine(p.ToString()); });
15 actionMethod2(3);
16
17 ActionMethodHandler actionMethod3 = new ActionMethodHandler(delegate(int p)
18 {
19 Console.WriteLine(p.ToString());
20 });
21 actionMethod3(3);
22
23 FuncTest.myAction = (p) => { Console.WriteLine(p.ToString()); };
24 FuncTest.myAction(3);
25
26 FuncTest.myAction = delegate(int p)
27 {
28 Console.WriteLine(p.ToString());
29 };
30 FuncTest.myAction(3);
31
32 Console.Read();
33 }
34
本文版权归作者靖芯科技和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                     
                    
                 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号