" Operator in Properties and MethodsIn C# code, the "=>" operator is used in expression-bodied members, which provide a..."/> " Operator in Properties and MethodsIn C# code, the "=>" operator is used in expression-bodied members, which provide a...">
Home >Backend Development >C++ >What's the Difference Between C#'s `=>` Operator in Properties and Field Initializers?
`` `` `` `Operator in Properties and Field Initializers? " /> <code class=" language-csharp>public int MaxHealth => Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0; </p> <p> The main attributes of the comparison expression and the field with the field of the field, we observe: </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>
The key difference is the way to get the value. The main attribute of the expression of the expression Getter is called every time the access attribute is accessed, and the field initialization device is only assigned once during instance creation. Not lambda expression
It should be noted that the main member of the expression is different from the Lambda expression. Lambda expression generates an entrusted instance or expression tree, and the main member of the expression of the expression indicates that the compiler is implemented with corresponding membership.
<code class="language-csharp">// 表达式主体成员属性 public int MaxHealth => x ? y : z; // 带有字段初始化器的字段 public int MaxHealth = x ? y : z;</code>
Extended to other member types
The main members of the expression can be applied to various membership types, including attributes, indexes, methods and operators. However, they are not suitable for nested types, events or fields.
The above is the detailed content of What's the Difference Between C#'s `=>` Operator in Properties and Field Initializers?. For more information, please follow other related articles on the PHP Chinese website!