Home >Backend Development >C++ >Expression-Bodied Members vs. Lambda Expressions: What's the Difference in C#'s `=>` Operator Usage?
`` Operator usage? " /> <code class=" language-csharp>public int MaxHealth => Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; </p> <p> <strong> </strong> Field initialization </p> <pre class="brush:php;toolbar:false"><code class="language-csharp">public int MaxHealth { get { return Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; } }</code> In the field initialization, the
=>
Wait for:
<code class="language-csharp">public int MaxHealth = x ? y : z;</code>
Key differences:
The key difference between<code class="language-csharp">public int MaxHealth; // 字段声明 public int MaxHealth = x ? y : z; // 字段赋值</code>The initialization of the expression member and the field is the scope and the timing of value. Expressive members define a attribute Getter (or method), which will be executed every time access attributes. On the other hand, the field initialization is set only once the field value is set when instantiated type.
Lambda expression
In the Lambda expression, theoperator is used to separate the input parameters of the anonymous function from its subject. Lambda expression is used to define anonymous method or expression that can be passed to other functions as parameters. They have nothing to do with expression members.
The above is the detailed content of Expression-Bodied Members vs. Lambda Expressions: What's the Difference in C#'s `=>` Operator Usage?. For more information, please follow other related articles on the PHP Chinese website!