将C#中方法签名中属性的返回值标记为只读。

huangapple go评论77阅读模式
英文:

Marking the return value of a property in a method signature as readonly in C#

问题

Visual Studio建议我在某些方法和属性的返回签名中添加readonly关键字(IDE 0251)。IDE 0251没有标题或描述,看起来已经在这里报告过了(https://github.com/dotnet/roslyn/issues/67949)。我列出这个以提供背景信息,但这个问题更多地涉及了readonly如何应用于方法或属性的返回值的基本含义。

以下是一个带有readonly的建议示例。我甚至没有意识到在方法的返回类型之前允许放置readonly是语言的一种特性,但这样编译和运行都没有问题。

在属性形式中:

public readonly bool IsInitialized => component != null;

在方法形式中:

public readonly bool IsInitialized()
{
    return component != null;
}

我在Microsoft的文档中花了一些时间来理解在这种情况下readonly的含义。我看到Microsoft提供了与将返回标记为ref readonly相关的文档,链接在这里:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly#ref-readonly-return-example。然而,我找不到一个只包含readonly术语的示例,附加到方法返回上。

ref readonly的示例有意义,因为“ref返回上的readonly修饰符表示返回的引用不能被修改”。

如果我将readonly应用于非ref返回,比如这个示例中的布尔值,这是什么意思?如果在我看到Visual Studio中的这条消息之前问我,我本来会认为这甚至不会编译。

请注意,Visual Studio中的消息是IDE0251,提供了一个空白的描述并提供了一个指向404页面的链接(https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0251)。尽管这个缺失的文档导致了一般的困惑,但我的问题是关于语言语法,而不是缺失的文档。

我正在使用Visual Studio 2022,版本17.6.4。

将C#中方法签名中属性的返回值标记为只读。

编辑:看起来这段代码在针对.net 6.0的新控制台应用程序项目中提供了一个编译错误,所以它只允许在我的成熟项目上编译,而不是在新的控制台应用程序上。感谢Peter B的反馈。

英文:

Visual studio is recommending I add the readonly keyword to the return signature of some methods and properties (IDE 0251). IDE 0251 has no title or description, which looks like it has been reported here (https://github.com/dotnet/roslyn/issues/67949). I listed this for context, but this issue is more about the underlying meaning of how readonly can be applied to a method or property return value.

Below is a sample of the recommendation with the readonly applied. I didn't realize it was even a feature of the language to allow placing readonly before the return type of a method, but this compiles and runs fine.

In property form:

public readonly bool IsInitialized => component != null;

In Method (Function) form:

public readonly bool IsInitialized()
{
    return component != null;
}

I spent some time in Microsoft's documentation to understand what readonly means in this context. I see where Microsoft provided documentation related to marking a return as ref readonly here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly#ref-readonly-return-example. However, I couldn't find an example of just the readonly term by itself attached to a method return.

The example of ref readonly makes sense, in that "The readonly modifier on a ref return indicates that the returned reference can't be modified."

What does it mean if I apply readonly to a non-ref return, such as the Boolean value in this example? If you had asked me before I saw this message in Visual Studio, I would have assumed this wouldn't even compile.

Note, the message in Visual Studio is IDE0251, which provides a blank description and provides a link to a 404 page (https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0251). While this missing documentation is contributing to the general confusion, my question is about the language syntax, not the missing documentation.

I am using Visual Studio 2022, Version 17.6.4.

将C#中方法签名中属性的返回值标记为只读。

EDIT: It looks like this code provides an compilation error in a fresh console application project targeting .net 6.0, so it only allows compiling on my mature project and not a fresh console application. Thank you Peter B for the feedback.

答案1

得分: 2

你将多个问题合并在一个帖子中,通常不应该这样做。无论如何,答案如下:

我没有意识到在方法的返回类型之前允许放置 readonly 是语言的一个特性

这是正确的,你可以在值类型 struct 中声明 readonly 成员函数。该修饰符的含义是你的函数不会更改实例的状态。你可以在这里了解更多信息。

请注意,Visual Studio 中的消息是 IDE0251,它提供了一个空白的描述,并提供了一个指向 404 页面的链接

这是正确的,并且已经被追踪为一个错误,详情请查看这里

看起来这段代码在一个新的 .net 6.0 控制台应用程序项目中提供了一个编译错误,所以它只允许在我的成熟项目上编译,而不是在一个新的控制台应用程序上

你可能尝试在一个 class 对象中复现它了?这是我最好的猜测,也是为什么通常应该用代码支持你的声明,这样你做了什么导致不同的结果就会立刻显而易见。

英文:

You squished a bunch of different questions in a single post, which you generally shouldn't do. Anyway, the answers are as such:

> I didn't realize it was even a feature of the language to allow placing readonly before the return type of a method

That's correct, you can declare readonly member functions in value type structs. The meaning of the modifier is that your function won't change the state of the instance. You can read more about this here.

> Note, the message in Visual Studio is IDE0251, which provides a blank description and provides a link to a 404 page

That is correct and is tracked as a bug here.

> It looks like this code provides an compilation error in a fresh console application project targeting .net 6.0, so it only allows compiling on my mature project and not a fresh console application

You probably tried to reproduce it in a class object? That's my best guess, and why you should generally back your claims with code -- it would be immediately obvious what you're doing to cause the different results.

huangapple
  • 本文由 发表于 2023年7月3日 05:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定