为什么我的本地变量可能会null,即使该方法返回了非null?
我不确定这是Visual Studio还是C#...
如果expression.parameter()
返回非零件parameterexpression
(无问号),那么,为什么要将其返回值保存为潜在的null的局部变量? ie parameterexpression?
而不是parameterexpression
我正在使用asp.net core 6.0.2和Visual Studio 2022 V17.1.3
I'm not sure if this is a bug in Visual Studio or C#...
If Expression.Parameter()
returns a non-null ParameterExpression
(no question mark), then why is a local variable that receives its return value being saved as potentially null? i.e. ParameterExpression?
instead of ParameterExpression
I'm using ASP.NET Core 6.0.2 and Visual Studio 2022 v17.1.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不能告诉你为什么语言设计师以这种方式设计它,但是至少它是记录的行为( https://learn.microsoft.com/en-us/dotnet/csharp/csharp/language-reference-reference-reference-reference-reference-reference/keywords/keywords/var ):
它与据称是智能的答案所暗示的vs无关,这仅仅是如何针对启用无效的注释上下文指定
var
关键字。Well, i can't tell you why the language designers designed it that way, but at least it's documented behavior (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var):
It has nothing to do with VS supposedly being smart as the other answer alludes to, it simply is how the
var
keyword is specified with respect to nullable annotation context being enabled.可能是新的VS足够聪明,可以知道表达式。参数可能会返回一个空。
尽管它无法返回可确定的类型,但回来的值也可能为null。
我相信.NET 6他们对无效类型进行了一些更改 /更严格。
< -sharpcorner.com/article/how-to to handle-nullable-reference-in-net-6/
It might be that the new VS is smart enough to know that Expression.Parameter might return a null..
Even though it's not returning a nullable type, the VALUE that comes back could be null.
I believe in .NET 6 they have done some changes / being more strict on nullable types.
https://www.c-sharpcorner.com/article/how-to-handle-nullable-reference-in-net-6/