" 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?

What's the Difference Between C#'s `=>` Operator in Properties and Field Initializers?

DDD
DDDOriginal
2025-01-30 23:06:12310browse

`` `` `` `Operator in Properties and Field Initializers? " /> What's the Difference Between C#'s `=>
</p> <> The difference between the operational character in the attribute and field initialization device <p>
<strong> C# code is used to express the main members of the expression, which provides a simple way to define attributes or methods. Unlike Lambda expressions, the main members of the expression generate specific members behind the scenes. <code>=></code>
</strong> <<> The attribute of </p> Getter <p> <code>=></code>
</p> The following code example illustrates the main attributes of the expression Getter: <p>
<strong>
<code>=></code> The compiler interprets this statement as a property, and its getter defines as follows: </strong>
</p>
<p> <字> The difference between the fields of the field initializer </p> <pre class=<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn