gpt4 book ai didi

c# - 为什么我的局部变量可能为空,即使该方法返回非空?

转载 作者:行者123 更新时间:2023-12-05 03:23:29 25 4
gpt4 key购买 nike

我不确定这是 Visual Studio 还是 C# 中的错误...

如果 Expression.Parameter() 返回一个非空的 ParameterExpression(无问号),那么为什么接收其返回值的局部变量被保存为潜在的无效的?即 ParameterExpression? 而不是 ParameterExpression

enter image description here

我正在使用 ASP.NET 6.0.2Visual Studio 2022 v17.1.3

最佳答案

好吧,我不能告诉你为什么语言设计者那样设计它,但至少它是有记录的行为(https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var):

When var is used with nullable reference types enabled, it always implies a nullable reference type even if the expression type isn't nullable. The compiler's null state analysis protects against dereferencing a potential null value. If the variable is never assigned to an expression that maybe null, the compiler won't emit any warnings. If you assign the variable to an expression that might be null, you must test that it isn't null before dereferencing it to avoid any warnings.

这与其他答案暗示的 VS 应该很聪明无关,它只是关于启用可为空注释上下文的 var 关键字的指定方式。

关于c# - 为什么我的局部变量可能为空,即使该方法返回非空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72509446/

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