Lambda Expressions Clarification

In the first lesson of Lambda Expressions, how are the lambda expressions being defined? will the first array parameter being placed as the method’s parameter?

This is the example used:

string[ ] spaceRocks = {“meteoroid”, “meteor”, “meteorite”};

  bool makesContact = Array.Exists(spaceRocks, (string s) => s == "meteorite");

So would the variable ‘s’ be defined by the elements in the ‘spaceRocks’ array?

I know this is a bit old now and you probably figured it out, but I know lambdas confuse a lot of people.

  1. How are the lands expressions being defined?
    A lambda has a pattern of (parameters) => {code body}.
    If there is one parameter being expected in then the () are not needed, if there are multiple then they will be needed. The => part is what says this is a lambda function. The code body is pretty much the same as a normal method code body, so you will do some logic in here and maybe return something, in this case a Boolean.

  2. Will the first array parameter being placed as the method’s parameter?
    Not 100% sure what you mean, so let me explain what is happening here behind the scenes (at a high level).
    For the case of Array.Exists the array will be looped over with each item being passed into the lambda and it is run until one item returns true or it has gone through the whole list. The array that is looped over is the first arguement passed I to Array.Exists, so spaceRocks.

  3. So would the variable ‘s’ be defined by the elements in the ‘spaceRocks’ array?
    Yes, s would represent an individual item in the spaceRocks array.

How a method that allows a lambda to be passed in will use it will vary and there should be some documentation explaining how it will be used. This allows you to know what will be passed to it and what logic to put into your code block.

If anyone stumbles upon this and found variable ‘s’, it is known as lambda expression s =>
The => operator is called the “lambda operator”.
Syntax:

(input-parameters) => { <sequence-of-statements> }

You can learn more here:

Lambda expression in C# (With Examples) - QA With Experts