Evenimente
17 mar., 23 - 21 mar., 23
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acumAcest browser nu mai este acceptat.
Faceți upgrade la Microsoft Edge pentru a profita de cele mai noi funcții, actualizări de securitate și asistență tehnică.
=>
) operator defines a lambda expressionThe =>
token is supported in two forms: as the lambda operator and as a separator of a member name and the member implementation in an expression body definition.
In lambda expressions, the lambda operator =>
separates the input parameters on the left side from the lambda body on the right side.
The following example uses the LINQ feature with method syntax to demonstrate the usage of lambda expressions:
string[] words = { "bot", "apple", "apricot" };
int minimalLength = words
.Where(w => w.StartsWith("a"))
.Min(w => w.Length);
Console.WriteLine(minimalLength); // output: 5
int[] numbers = { 4, 7, 10 };
int product = numbers.Aggregate(1, (interim, next) => interim * next);
Console.WriteLine(product); // output: 280
Input parameters of a lambda expression are strongly typed at compile time. When the compiler can infer the types of input parameters, like in the preceding example, you can omit type declarations. If you need to specify the type of input parameters, you must do that for each parameter, as the following example shows:
int[] numbers = { 4, 7, 10 };
int product = numbers.Aggregate(1, (int interim, int next) => interim * next);
Console.WriteLine(product); // output: 280
The following example shows how to define a lambda expression without input parameters:
Func<string> greet = () => "Hello, World!";
Console.WriteLine(greet());
For more information, see Lambda expressions.
An expression body definition has the following general syntax:
member => expression;
Where expression
is a valid expression. The return type of expression
must be implicitly convertible to the member's return type. If the member:
void
return type orset
accessorexpression
must be a statement expression. Because the expression's result is discarded, the return type of that expression can be any type.
The following example shows an expression body definition for a Person.ToString
method:
public override string ToString() => $"{fname} {lname}".Trim();
It's a shorthand version of the following method definition:
public override string ToString()
{
return $"{fname} {lname}".Trim();
}
You can create expression body definitions for methods, operators, read-only properties, constructors, finalizers, and property and indexer accessors. For more information, see Expression-bodied members.
The =>
operator can't be overloaded.
For more information about the lambda operator, see the Anonymous function expressions section of the C# language specification.
Feedback pentru .NET
.NET este un proiect open source. Selectați un link pentru a oferi feedback:
Evenimente
17 mar., 23 - 21 mar., 23
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acum