/*******
* Action、Func是简化的委托,不同的是Action没有返回值,Func是有返回值
*******/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class TestClass
{
public static void test1(string str)
{
Console.WriteLine("test1方法:" + str);
}
public static string test2(string str)
{
return "test2方法:" + str;
}
public static void test()
{
TestDelegate.test();
TestAction.test();
TestFunc.test();
}
}
public class TestDelegate
{
private delegate string testDele(string str1);
public static void test()
{
Console.WriteLine("测试委托开始");
testDele del = new testDele(TestClass.test2);
string str = del("tom");
Console.WriteLine(str);
Console.WriteLine("测试委托结束");
}
}
public class TestAction
{
public static void test()
{
Console.WriteLine("测试Action开始");
Action<string> action = TestClass.test1;
action("joyet");
Console.WriteLine("测试Action结束");
}
}
public class TestFunc
{
public static void test()
{
Console.WriteLine("测试Func结束");
Func<string, string> fun = TestClass.test2;
string str = fun("tom");
Console.WriteLine(str);
Console.WriteLine("测试Func结束");
}
}
}