C# 第一次使用lambda。如果可能,希望将更多代码内联

C# 第一次使用lambda。如果可能,希望将更多代码内联,c#,winforms,lambda,C#,Winforms,Lambda,我目前正试图教自己如何使用lambda表达式(非常喜欢它们的外观) 在我的win表单上,我有两个用于浏览目录的按钮,然后将用户选择的目录放置在相应的文本框中。这就是我目前所拥有的 在setDirectory事件中,是否有任何方法可以内联执行按钮检查?我真的希望清理setDirectory事件中的代码 private void setDirectory_Click(object sender, EventArgs e) { Button dir

我目前正试图教自己如何使用lambda表达式(非常喜欢它们的外观)

在我的win表单上,我有两个用于浏览目录的按钮,然后将用户选择的目录放置在相应的文本框中。这就是我目前所拥有的

在setDirectory事件中,是否有任何方法可以内联执行按钮检查?我真的希望清理setDirectory事件中的代码

        private void setDirectory_Click(object sender, EventArgs e)
        {
           Button dirButton = sender as Button;
           TextBox dirTextBox;

           if (dirButton.Name == fromDirButton.Name)
               dirTextBox = fromTextBox;
           else
               dirTextBox = toTextBox;

           Func<string> setDirectory = SetDirectory();
           fromTextBox.Invoke((MethodInvoker)(() => { dirTextBox.Text = setDirectory(); }));
        }

        private static Func<string> SetDirectory()
        {
           using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
           {
               folderBrowserDialog.Description = "Select Path";
               folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
               folderBrowserDialog.ShowDialog();
               return () => folderBrowserDialog.SelectedPath != "" ? folderBrowserDialog.SelectedPath : "";
           }
        }
private void setDirectory\u单击(对象发送方,事件参数e)
{
Button dirButton=发送方为按钮;
文本框dirTextBox;
if(dirButton.Name==fromDirButton.Name)
dirTextBox=fromTextBox;
其他的
dirTextBox=toTextBox;
Func setDirectory=setDirectory();
fromTextBox.Invoke((MethodInvoker)(()=>{dirTextBox.Text=setDirectory();}));
}
私有静态Func SetDirectory()
{
使用(FolderBrowserDialog FolderBrowserDialog=新建FolderBrowserDialog())
{
folderBrowserDialog.Description=“选择路径”;
folderBrowserDialog.RootFolder=Environment.SpecialFolder.MyComputer;
folderBrowserDialog.ShowDialog();
return()=>folderBrowserDialog.SelectedPath!=''?folderBrowserDialog.SelectedPath:'';
}
}

对于Lambda来说,您的使用并不十分理想,Lambda在Linq&Generics中的使用非常方便。例如:

List<string> collection = new List<string>();
if(!collection.Any())
     // Collection isn't null.

var quantity = collection.Count(c => c > 5);
列表集合=新列表();
如果(!collection.Any())
//集合不是空的。
变量数量=收集计数(c=>c>5);
正如您所看到的,Lambda通过管道传输数据,这很有意义。它变得富有表现力,而不是模糊不清

在可能使用Lambda的领域,使用更传统的实现可能会更好。只是因为它变得越来越复杂,表达能力越来越差。这使得代码更难维护和重构,简单性通常是最好的方法。


我建议尝试在适当的环境中学习Lambda,而不是为了使用它们。

如果可能的话,寻找更多的内联代码

为什么??这看起来不像是一个正确的使用

你的问题应该是:

何时使用Lambda表达式,何时不使用?

Lambda表达式不会取代对话框等传统编码。它们提供了一种更实用的方式来表达正在进行的操作

var net30PastDueInvoices = 
    invoiceRepository.GetInvoicesFor(currentClient)
        .Where(f => 30 < DateTime.Today.AddDays(-f.InvoiceDate));
您尝试使用Lambda表达式的方式非常不受欢迎,因为它只会增加代码的复杂性

简而言之,Lambda Expression非常实用,可以用函数的方式表达业务规则等。除此之外

免责声明


我不是说你很蠢。按照链接了解更多信息。

这似乎不是lambda功能的一个很好的用途。为什么不使用传统的方法呢?如上所述,这应该是一个单一的常规方法,只是尝试学习如何使用lambda。这纯粹是educational@AceK47如果您想学习如何使用特定功能,您应该将其用于该工具旨在解决的问题。试图用它来解决它不能解决的问题只会教会你如何不正确地使用它,而不是如何正确地使用它。你在
SetDirectory
中使用lambda表达式是不正确的:它捕获一个已处理的对象。是的,基本上就是我所暗示的。回答得好-
var items = repository.GetList().ForEach(i => {
        // Process items here.
        // Notice that the code processed here is located in a foreach loop
        // especially for the given purpose within an anonymous method.
        // Should you need to perform this operation over and over again,
        // you should extract this to a proper method and describe what it 
        // does in its name.
    });