C#中的Action<>和Func<>
来源:互联网 发布:编程打印空心字符菱形 编辑:程序博客网 时间:2024/04/26 08:33
其实他们两个都是委托【代理】的简写形式。
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<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>
- C#中的Action<>和Func<>和Predicate
- C# 中的Action Func
- 关于c#中的Func<>和Action<>
- C# Action和Func
- c#委托Action和Func
- C#的Action<>和Func<>
- C# Action和Func委托
- C#中的Action<>和Func<>以及传统的委托
- C#中的Action<T>、Func<T>委托和Lambda表达式
- RDLC报表(五)
- Android 内存泄漏调试
- 用Universal USB Installer 制作Linux 安装U盘
- 编程珠玑(2)第九章学习笔记
- ubuntu环境下工作命令积累
- C#中的Action<>和Func<>
- Google Maps Android v1 API Key
- P6. zipfile
- 进程介绍(一)
- wiki
- Configuring Auditing in SharePoint 2010
- 最近写的一个正则表达式验证器,很简单!很简单!
- Tiny210v2( S5PV210 ) 平台下 FIMD 对应 的 framebuffer 驱动中,关于 video buffer 的理解
- 朋友