简体   繁体   English

解释c#lambda语法

[英]Interpret c# lambda syntax

I have a general understanding of Lambda expressions but not sure what the () => syntax means. 我对Lambda表达式有一般的了解,但不确定() =>语法是什么意思。 This code appears to return a list of Task items but I'm unsure how it executes or how to interpret what it means. 此代码似乎返回任务项列表,但我不确定它是如何执行或如何解释它的含义。

Can someone tell me: 有人能告诉我:

  1. what does () => mean? 什么() =>是什么意思?
  2. It appears that each of the new Task blocks are executed sequentially? 似乎每个new Task块都是按顺序执行的?
 private DateTime? _myTime = null;
 private DateTime? _theirTime = null;
 private bool _okToProcess = true;

 List<Task> myTasks = new List<Task>
            {
                   new Task( () => 
                    {
                        _myTime = GetMyTime();
                    }),

                     new Task( () => 
                    {
                        _theirTime = GetTheirTime(); 
                        _okToProcess = _myTime > _theirTime;                                         
                    }),

                new Task(() => 
                    {
                        if (_okToProcess)
                        {
                           WriteToMyLogStep("We are processing");
                        }
                        else
                        {
                           WriteToMyLogStep("We are NOT processing");
                        }
                     });
            };

() - represents the parameters taken by the anonymus method, () - 表示匿名方法采用的参数,

=> - is generally read goes to and points to the anonymus methods body that uses those parameters (if any provided). => -通常读进入并指向一个使用这些参数(如果有的话提供)anonymus方法主体。

In your case the lamda expression is the same as: 在您的情况下,lamda表达式与以下内容相同:

 delegate() {  _myTime = GetMyTime(); }

As for the Tasks they are just added to a list, they are not executed. 至于任务,它们只是添加到列表中,它们不会被执行。 How they will be executed depends on how you want to execute them in the future. 它们的执行方式取决于您将来如何执行它们。 (maybe in a loop on the same thread or maybe on different threads). (可能在同一个线程或可能在不同线程上的循环中)。

The () => syntax just means that there's no named input to the lambda. () =>语法只意味着lambda没有命名输入。 It is like calling a method that takes no parameters. 这就像调用一个不带参数的方法。

As for the code the tasks are just created but never actually started, so they don't run in the code shown. 至于代码,任务刚刚创建但从未实际启动过,因此它们不会在显示的代码中运行。 I assume the list is enumerated and the tasks started somewhere else. 我假设列表已枚举,任务在其他地方启动。

You're probably used to seeing lambda functions like x => DoSomething(x) . 您可能习惯于看到像x => DoSomething(x)这样的lambda函数。 That's actually shorthand for (x) => DoSomething(x) - the initial (x) represents the parameters to the function, the same as the (x) in DoSomething(x) . 这实际上是用于速记(x) => DoSomething(x) -初始(x)表示的参数的功能,等同于(x)DoSomething(x) does. 确实。

In this case, there are no parameters, so it uses () , the same as a regular function would be DoSomething() . 在这种情况下,没有参数,所以它使用() ,与常规函数相同的是DoSomething()

As for the Task s, they'll run in parallel once started, unless you explicitly wait until each one is done before starting the next. 对于Task s,它们将在启动后并行运行,除非您明确等到每个任务完成后才开始下一个。

()类似于函数所采用的参数,我相信任务是异步执行的=>指向函数体的点

  1. The anonymous Action to be executed; 要执行的匿名行动;
  2. The tasks are only defined, not executed. 任务仅定义,而不是执行。

()=> is an Action - an expression with no parameters. ()=>是一个Action - 一个没有参数的表达式。

These tasks have not been started so (currently) will never complete... 这些任务尚未开始,因此(目前)永远不会完成......

  1. () is your list of parameters, in your case empty or none, that is getting passed into the anonymous function following the => operator. ()是您的参数列表,在您的情况下为空或无,将被传递到=>运算符后面的匿名函数。 The => gets its name from Lambda calculus. =>从Lambda演算中得到它的名字。

  2. It depends on how you are calling your tasks in the list. 这取决于您在列表中调用任务的方式。 This is simply a List<Task> with Tasks in it. 这只是一个带有任务的List<Task> In order to execute your tasks you would need to do something like this: 为了执行您的任务,您需要执行以下操作:

     foreach (Task t in myTasks) { t.RunSynchronously(); // in this case the tasks would run synchronously } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM