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
Release: 2025-01-30 23:06:12
Original
407 people have browsed it

`` `` `` `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> <div class=

public int MaxHealth => 
         Memory[Address].IsValid ? 
         Memory[Address].Read<int>(Offs.Life.MaxHp) : 
         0;
Copy after login

The main attributes of the comparison expression and the field with the field of the field, we observe:

public int MaxHealth
{
    get
    {
        return Memory[Address].IsValid ? Memory[Address].Read<int>(Offs.Life.MaxHp) : 0;
    }
}
Copy after login

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.
// 表达式主体成员属性
public int MaxHealth => x ? y : z;

// 带有字段初始化器的字段
public int MaxHealth = x ? y : z;
Copy after login

<扩> 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 of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template