In-depth understanding of the Lambda operator (=>
) in C#
Question:
What does the syntax in C# stand for? =>
Answer: The
syntax represents the Lambda operator. This operator was introduced in C# 3 and has multiple uses. =>
Lambda expression (C# 3-5):
In this context, is used to create Lambda expressions. These expressions are a concise form of anonymous methods, allowing you to write anonymous functions as a single expression. For example: =>
Func<Person, string> nameProjection = p => p.Name;
Func<Person, string> nameProjection = delegate (Person p) { return p.Name; };
Expression body member (C# 6):
In C# 6, the same syntax is used for expression body members. These are properties or methods whose bodies are defined as expressions rather than blocks of code. For example: =>
// 表达式主体属性 public int IsValid => name != null && id != -1; // 表达式主体方法 public int GetHashCode() => id.GetHashCode();
More resources:
The above is the detailed content of What does the `=>` operator mean in C#?. For more information, please follow other related articles on the PHP Chinese website!