在C#中了解Action委托后,我一直在寻找可以在代码中最好使用它的方法。我想出了这个模式:.NET中的传统循环与动作委托
Action<string> DoSomething = (lSomething) =>
{
// Do something
};
DoSomething("somebody");
DoSomething("someone");
DoSomething("somewhere");
如果我已经使用了传统的循环,它看起来是这样的:
List<string> lSomeList = new List<string>();
lSomeList.Add("somebody");
lSomeList.Add("someone");
lSomeList.Add("somewhere");
foreach (string lSomething in lSomeList)
{
// Do something
}
有没有两者之间有什么明显的区别?对我而言,他们看起来同样容易理解和维护,但是我可以使用其他一些标准来区分何时可以优先于其他人吗?