你如何使用Func键<>和行动<>在设计应用程序时? [英] How do you use Func<> and Action<> when designing applications?
问题描述
我可以找到有关Func键和LT所有的例子;>和Action<>的简单在下面,你看到一个的如何他们在技术上的工作,但我想看到他们在那里他们解决以前不能得到解决或者可以在一个更复杂的方式才能解决的问题,即我知道他们是如何工作的,我可以看到他们是示例中使用的简洁而有力,所以我想了解他们,他们解决什么样的问题, 更大的意义上,我怎么能在应用程序的设计中使用它们。
以何种方式(模式)你使用Func键<>和行动<?>解决实际问题
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间TestFunc8282
{
类节目
{
静态无效的主要(字串[] args)
{
// FUNC与代表
Func键<字符串,字符串>转换=委托(字符串s)
{
返回s.ToUpper();
};
// FUNC与拉姆达
Func键<字符串,字符串> convert2 = S = GT; s.Substring(3,10);
//动作
动作< INT,串> recordIt =(I,标题)=>
{
Console.WriteLine(--- {0},标题);
Console.WriteLine(增加五到{0}:我);
Console.WriteLine第(i + 5);
};
Console.WriteLine(转换(这是第一次测试。));
Console.WriteLine(convert2(这是第二次测试。));
recordIt(5,第一招);
recordIt(3,第二个);
到Console.ReadLine();
}
}
}
他们也得心应手重构switch语句
看看下面的(虽然简单)例如:
公共无效移动(INT距离,方向方向)
{
开关(方向)
{
案方向.UP:
Position.Y + =距离;
中断;
情况下Direction.Down:
Position.Y - =距离;
中断;
情况下Direction.Left:
Position.X - =距离;
中断;
情况下Direction.Right:
Position.X + =距离;
中断;
}
}
随着Action委托,您可以按如下重构它
静态的东西()
{
_directionMap =新词典<方向,动作<定位,诠释> ;>
{
{Direction.Up,(方位,距离)=> position.Y + =距离}
{Direction.Down,(方位,距离)=> position.Y - =距离}
{Direction.Left,(方位,距离)=> position.X - =距离}
{Direction.Right,(方位,距离)=> position.X + =距离}
};
}
公共无效移动器(INT距离,方向方向)
{
_directionMap [方向](this.Position,距离);
}
All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. I know how they work and I can see they are terse and powerful, so I want to understand them in a larger sense of what kinds of problems they solve and how I could use them in the design of applications.
In what ways (patterns) do you use Func<> and Action<> to solve real problems?
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();
}
}
}
They're also handy for refactoring switch statements.
Take the following (albeit simple) example:
public void Move(int distance, Direction direction)
{
switch (direction)
{
case Direction.Up :
Position.Y += distance;
break;
case Direction.Down:
Position.Y -= distance;
break;
case Direction.Left:
Position.X -= distance;
break;
case Direction.Right:
Position.X += distance;
break;
}
}
With an Action delegate, you can refactor it as follows:
static Something()
{
_directionMap = new Dictionary<Direction, Action<Position, int>>
{
{ Direction.Up, (position, distance) => position.Y += distance },
{ Direction.Down, (position, distance) => position.Y -= distance },
{ Direction.Left, (position, distance) => position.X -= distance },
{ Direction.Right, (position, distance) => position.X += distance },
};
}
public void Move(int distance, Direction direction)
{
_directionMap[direction](this.Position, distance);
}
这篇关于你如何使用Func键<>和行动<>在设计应用程序时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!