4

I have a general understanding of Lambda expressions but not sure what the () => syntax means. 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?
 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");
                        }
                     });
            };
2
  • 2
    1: the action to be executed; 2: the tasks are only defined, not executed. Commented Feb 22, 2013 at 16:37
  • 1
    You shouldn't ask two distinct questions at the same time. Next time, actually post two different questions. Commented Feb 22, 2013 at 16:59

7 Answers 7

4

() - 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).

In your case the lamda expression is the same as:

 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).

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure if explaining lambdas using the old delegate syntax (which is not used much) is helpful.
well, old delegate syntax resembles more to a normal method compared to lambdas, thus you can get a better picture. I'm talking from an unexperienced point of view. wouldn't you agree?
Great answer Freeman!... and this helped me to better understand this syntax and put things in perspect. Thanks
4

The () => syntax just means that there's no named input to the 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.

1 Comment

Thanks @BrianRasmussen. Each task IS executed using the Start method later in the code thru an enumeration of the list.
2

You're probably used to seeing lambda functions like x => DoSomething(x). 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). does.

In this case, there are no parameters, so it uses (), the same as a regular function would be DoSomething().

As for the Tasks, they'll run in parallel once started, unless you explicitly wait until each one is done before starting the next.

1 Comment

thx for the clarification... (the Tasks) "they'll run in parallel once started, unless you explicitly wait until each one is done". Correct, each task is started in an enumeration like: task.Start; task.Wait();
0

() would be similar to the parameters the function takes, and I believe that tasks are executed asynchronously => Points to the body of the function

Comments

0
  1. The anonymous Action to be executed;
  2. The tasks are only defined, not executed.

Comments

0

()=> is an Action - an expression with no parameters.

These tasks have not been started so (currently) will never complete...

Comments

0
  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.

  2. It depends on how you are calling your tasks in the list. This is simply a List<Task> with Tasks in it. 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
    }
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.