For what is "=" in C #

6

My question is so that the characters are used = > in C #, I've seen it in a method to access the device's light sensor. This is the method

private void _lightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
    {
        _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            Lux = args.Reading.IlluminanceInLux;
        });

        Debug.WriteLine("Lux: {0}", Lux);
    }

Thank you.

    
asked by Edwin V 06.02.2016 в 00:06
source

2 answers

7

In c # the operator => is known as the lambda operator.

Used to create a Func<> , Action<> or Expression<Func<>> as needed.

In your case

() =>
    {
        Lux = args.Reading.IlluminanceInLux;
    }

It is equivalent to a method similar to this

void MetodoAPasarleAlRunAsync()
{
    Lux = args.Reading.IlluminanceInLux;
}

To the left of the => the argument list in this case is empty and the code block to be executed is right.

Since there are only a couple of empty parentheses on the left, the method has no parameters and since the code block does not return anything, its return type will be void

    
answered by 06.02.2016 / 00:32
source
6

Lambda operator = >

  

The token = > it is called a lambda operator. Used in expressions   lambda to separate the input variables from the left side of the   lambda body on the right side. Lambda expressions are expressions   inserted similar to anonymous methods, but more flexible; he   they use a lot in LINQ queries that are expressed in the syntax   of the method. For more information, see Lambda Expressions (Guide   of programming C #).

Operator documentation = > in Spanish.

    
answered by 06.02.2016 в 00:31