gpt4 book ai didi

c# - 属性在一台机器上工作正常,但在另一台机器上出错

转载 作者:太空宇宙 更新时间:2023-11-03 18:54:07 27 4
gpt4 key购买 nike

我的代码是这样写的:

public bool Expanded { get => expanded; set => expanded = value; }

在一台机器上它工作正常但在另一台机器上它全是红色突出显示(“并非所有代码路径都返回一个值”)

当我这样重写它时它工作正常:

public bool Expanded
{
get { return expanded; }
set { expanded = value; }
}

但我不想重写它。

在两台机器上我都使用 .NET Framework 4.7.1

您知道什么地方可能出错吗?

最佳答案

在 c# 7.0 中添加了表达式主体属性访问器,在 c# 6.0 中,您只能将表达式主体用于方法。
自动属性初始化器 (int Count {get;} = 1;) 不要与表达式主体属性 (int Count {get => return 1;}) 混淆.

来自 What's new in C# 7

C# 6 introduced expression-bodied members for member functions, and read-only properties.
C# 7 expands the allowed members that can be implemented as expressions. In C# 7, you can implement constructors, finalizers, and get and set accessors on properties and indexers.

(强调我的)

关于c# - 属性在一台机器上工作正常,但在另一台机器上出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49732223/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com