我能找到的关于 Func<> 和 Action<> 的所有示例都很简单,如下所示,您可以在其中看到它们在技术上是如何工作的,但我希望看到它们用于解决以前无法解决或无法解决的问题的示例只能以更复杂的方式解决,即我知道它们是如何工作的,并且我可以看到它们简洁而强大,所以我想从更大的意义上理解它们,它们解决了什么样的问题以及我如何在应用程序的设计。
您以何种方式(模式)使用 Func<> 和 Action<> 来解决实际问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestFunc8282
{
class Program
{
static void Main(string[] args)
{
//func with delegate
Func<string, string> convert = delegate(string s)
{
return s.ToUpper();
};
//func with lambda
Func<string, string> convert2 = s => s.Substring(3, 10);
//action
Action<int,string> recordIt = (i,title) =>
{
Console.WriteLine("--- {0}:",title);
Console.WriteLine("Adding five to {0}:", i);
Console.WriteLine(i + 5);
};
Console.WriteLine(convert("This is the first test."));
Console.WriteLine(convert2("This is the second test."));
recordIt(5, "First one");
recordIt(3, "Second one");
Console.ReadLine();
}
}
}