Page 169 - CSharp/C#
P. 169

or explicitly type the parameter but then parenthesis are required:


         .Select( (int number) => number * number);


        The lambda body is an expression and has an implicit return. You can use a statement body if you
        want as well. This is useful for more complex lambdas.


         .Select( number => { return number * number; } );


        The select method returns a new IEnumerable with the computed values.


        The second lambda expression sums the numbers in list returned from the select method.
        Parentheses are required as there are multiple parameters. The types of the parameters are
        explicitly typed but this is not necessary. The below method is equivalent.


         .Aggregate( (first, second) => { return first + second; } );


        As is this one:


         .Aggregate( (int first, int second) => first + second );


        Anonymous types


        Anonymous types provide a convenient way to encapsulate a set of read-only properties into a
        single object without having to explicitly define a type first. The type name is generated by the
        compiler and is not available at the source code level. The type of each property is inferred by the
        compiler.


        You can make anonymous types by using the new keyword followed by a curly brace ({). Inside the
        curly braces, you could define properties like on code below.


         var v = new { Amount = 108, Message = "Hello" };


        It's also possible to create an array of anonymous types. See code below:


         var a = new[] {
             new {
                 Fruit = "Apple",
                 Color = "Red"
             },
             new {
                 Fruit = "Banana",
                 Color = "Yellow"
             }
         };


        Or use it with LINQ queries:


         var productQuery = from prod in products



        https://riptutorial.com/                                                                             115
   164   165   166   167   168   169   170   171   172   173   174