【发布时间】:2022-01-09 21:23:27
【问题描述】:
我最近发现在新版本的 C# 中存在所谓的表达式体成员,如 here 所述。
表达式主体成员的示例是(仅适用于 getter 属性):
private int _x;
public int X
{
get => _x;
}
上面的表达式主体成员是否等同于旧的 C# 版本?
private int _x;
public int X
{
get
{
return _x;
}
}
【问题讨论】:
-
它们并不是什么“新东西”——它们从大约十年前的 C# 6.0 就已经存在了。
-
每当你想知道一个特性是否编译相同时,只要问 Sharplab sharplab.io/…
-
@Martheen 很棒的工具!非常感谢。我不知道。从现在开始我会考虑的。
-
@Dai Ops,直到现在我才听说它;)
标签: c# properties getter expression-bodied-members