C#中的Action<>和Func<>
2013-04-15 16:51
351 查看
其实他们两个都是委托【代理】的简写形式。
一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp]
public delegate void myDelegate(string str);
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
myDelegate d = new myDelegate(HellowChinese);
d("Mr wang");
用了Action之后呢:
[csharp]
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
Action<string> action = HellowChinese;
action("Spring.");
就是相当于省去了定义委托的步骤了。
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp]
public static string HelloEnglish(string strEnglish)
{
return "Hello." + strEnglish;
}
Func<string, string> f = HelloEnglish;
Console.WriteLine(f("Srping ji"));
Console.ReadLine();
private void button5_Click(object sender, EventArgs e)
{
//类似委托功能
Func<string, int> test = TsetMothod;
Console.WriteLine(test("123"));
Func<string, int> test1 = TsetMothod;
//只需要调用这个类就可以减少重复的代码
CallMethod<string>(test1, "123");
//或者采用这种
CallMethod<string>(new Func<string, int>(TsetMothod), "123");
CallMethod(new Func<string, int>(TsetMothod), "123");
}
public static void CallMethod<T>(Func<T, int> func, T item)
{
try
{
int i = func(item);
Console.WriteLine(i);
}
catch (Exception e)
{
}
finally
{
}
}
public static int TsetMothod(string name)
{
if (string.IsNullOrEmpty(name))
{
return 1;
}
return 0;
}
----------------------------------Func例-------------------------------------------
private void button6_Click(object sender, EventArgs e)
{
// Note that each lambda expression has no parameters.
LazyValue<int> lazyOne = new LazyValue<int>(() => ExpensiveOne());
LazyValue<long> lazyTwo = new LazyValue<long>(() => ExpensiveTwo("apple"));
Console.WriteLine("LazyValue objects have been created.");
// Get the values of the LazyValue objects.
Console.WriteLine(lazyOne.Value);
Console.WriteLine(lazyTwo.Value);
}
static int ExpensiveOne()
{
Console.WriteLine("\nExpensiveOne() is executing.");
return 1;
}
static long ExpensiveTwo(string input)
{
Console.WriteLine("\nExpensiveTwo() is executing.");
return (long)input.Length;
}
}
class LazyValue<T> where T : struct
{
private Nullable<T> val;
private Func<T> getValue;
// Constructor.
public LazyValue(Func<T> func)
{
val = null;
getValue = func;
}
public T Value
{
get
{
if (val == null)
// Execute the delegate.
val = getValue();
return (T)val;
}
}
}
一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp]
public delegate void myDelegate(string str);
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
myDelegate d = new myDelegate(HellowChinese);
d("Mr wang");
用了Action之后呢:
[csharp]
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
Action<string> action = HellowChinese;
action("Spring.");
就是相当于省去了定义委托的步骤了。
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp]
public static string HelloEnglish(string strEnglish)
{
return "Hello." + strEnglish;
}
Func<string, string> f = HelloEnglish;
Console.WriteLine(f("Srping ji"));
Console.ReadLine();
private void button5_Click(object sender, EventArgs e)
{
//类似委托功能
Func<string, int> test = TsetMothod;
Console.WriteLine(test("123"));
Func<string, int> test1 = TsetMothod;
//只需要调用这个类就可以减少重复的代码
CallMethod<string>(test1, "123");
//或者采用这种
CallMethod<string>(new Func<string, int>(TsetMothod), "123");
CallMethod(new Func<string, int>(TsetMothod), "123");
}
public static void CallMethod<T>(Func<T, int> func, T item)
{
try
{
int i = func(item);
Console.WriteLine(i);
}
catch (Exception e)
{
}
finally
{
}
}
public static int TsetMothod(string name)
{
if (string.IsNullOrEmpty(name))
{
return 1;
}
return 0;
}
----------------------------------Func例-------------------------------------------
private void button6_Click(object sender, EventArgs e)
{
// Note that each lambda expression has no parameters.
LazyValue<int> lazyOne = new LazyValue<int>(() => ExpensiveOne());
LazyValue<long> lazyTwo = new LazyValue<long>(() => ExpensiveTwo("apple"));
Console.WriteLine("LazyValue objects have been created.");
// Get the values of the LazyValue objects.
Console.WriteLine(lazyOne.Value);
Console.WriteLine(lazyTwo.Value);
}
static int ExpensiveOne()
{
Console.WriteLine("\nExpensiveOne() is executing.");
return 1;
}
static long ExpensiveTwo(string input)
{
Console.WriteLine("\nExpensiveTwo() is executing.");
return (long)input.Length;
}
}
class LazyValue<T> where T : struct
{
private Nullable<T> val;
private Func<T> getValue;
// Constructor.
public LazyValue(Func<T> func)
{
val = null;
getValue = func;
}
public T Value
{
get
{
if (val == null)
// Execute the delegate.
val = getValue();
return (T)val;
}
}
}
相关文章推荐
- C#中的Action<T>、Func<T>委托和Lambda表达式
- C#的Action<>和Func<>
- c#中Action<T>和Func<T>委托
- 提问c# action<> func<> 这2个委托怎么用和理解
- C#中的Action<>和Func<>以及传统的委托
- C#中的Action<>和Func<>
- C#的委托 Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#的委托 Action<>和Func<>
- Action<T>和Func<T>泛型委托
- C#基础:委托之Action<T>和Func<T>的用法
- C# Action<T>、Func<T>、Predicate<T>委托的区别、用法
- C# Action<T> 委托
- Func<TResult>委托,Action 委托,Tuple 类
- C#中的Action<>和Func<> 委托区别
- C# Func<T>、Action<T> 的区别于说明
- 【基础】C#:委托Action、Action<T>、Func<T>、Predicate<T>
- 浅析C#中的Func<>委托
- System.Func<>与System.Action<>