2

so I am looking at this C# method

if (IsInDesignMode)
        {
            // Only runs in design mode
            _dataService.GetMachine(_machines[0].Machine.SerialNumber, (machine, error) => //<-- this is what I am confused about
            {
                SelectedMachine = new MachineViewModel(machine);
            });
        }

I understand the if() statement and the SelectedMachine = new MachineViewModel(machine); line. But I am confused about the commented line.

_dataService calls a GetMachine method passing in _machines[0].Machine.SerialNumber param and (machine, error) => {}. It is not an "equal or less than" statement right?. It kinda looks like a Javascript code to me...?

Does the method say,

If IsInDesignMode {
      dataservice.GetMachine(machine serial number, machine error is new MachineViewModel)
}

Can any one explain what => { } this is? thank you very much!

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
jmesolomon
  • 563
  • 5
  • 15
  • Here's a couple of links for you: http://msdn.microsoft.com/en-us/library/bb311046.aspx http://msdn.microsoft.com/en-us/library/bb397687.aspx – Blorgbeard Nov 04 '14 at 02:06
  • @RaymondChen Hi Raymond, yes you are right! I just clicked on the link. Thanks! i'll search stackoverflow even harder next time :) – jmesolomon Nov 04 '14 at 02:49

3 Answers3

3

The part you are asking about is an anonymous method that uses a lambda expression. It is commonly used in callbacks.

When you write this

(machine, error) => { SelectedMachine = new MachineViewModel(machine); }

you are making a function that has no name (and therefore cannot be reused by name, like a regular method). It is very convenient in situations when you need to produce a piece of callable code that needs to be used only once, e.g. in callbacks.

Note that the method does not have to be anonymous: you could make an equivalent named method. However, an since the anonymous method is built in the context of the method where it is used, the variables from the context are available to it. Your anonymous method assigns SelectedMachine, which is probably a property of your class. In the same way, anonymous methods can access local variables as well, which is a very powerful mechanism of combining together a state and a piece of code that operates on it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Might the phrase "and therefore cannot be reused" be misleading? – Daniel Pratt Nov 04 '14 at 02:11
  • @DanielPratt if I create a named delegate e.g `del customDelegate => a= a+b` then I could reuse customDelegate function.. is this right? so I guess a nameless function cannot be re-used whereas a named function can. – jmesolomon Nov 04 '14 at 02:16
  • @jmesolomon I think Daniel meant that if you assign an anonymous function to a variable of delegate type and pass that variable around, you would be able to reuse that function by accessing it through a variable. Named methods can be reused both by name and through a delegate. – Sergey Kalinichenko Nov 04 '14 at 02:20
  • @dasblinkenlight thanks for clearing that out. ^^, – jmesolomon Nov 04 '14 at 02:22
1

To be more precise. It is an Anonymous method using lambda expression. the sign you are asking '=> { }' is called lambda expression. Usually it is used with Delegate type like func, Action, predicate and others. Have a look on the above types to make yourself more clear.

maxspan
  • 13,326
  • 15
  • 75
  • 104
0

It is a lambda expression. Have a look on this page for more info: http://msdn.microsoft.com/en-us/library/bb397687.aspx

Jyrka98
  • 530
  • 1
  • 10
  • 19