[英]Why does not Visual Studio display warnings generated by the #warning directive (CS1030)?
[英]Why does Visual Studio generate warning CS620 when passing KeyValuePair enumeration to FormUrlEncodedContent constructor?
考虑以下代码:
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace dla.test2{
internal class Test{
public static void Main(){
var map=new Dictionary<string,string>(){
["hello"]="world"
};
using var foo=new FormUrlEncodedContent(map);
}
}
}
调用FormUrlEncodedContent
的构造函数会在构建时产生以下编译器警告:
警告CS8620类型“Dictionary<string, string>”的参数不能用于“FormUrlEncodedContent.FormUrlEncodedContent(IEnumerable<KeyValuePair<string?, string”中的“IEnumerable<KeyValuePair<string?, string?>>”类型的参数“nameValueCollection” ?>> nameValueCollection)' 由于引用类型的可空性不同。
FormUrlEncodedContent的文档表明构造函数应该接受IEnumerable<KeyValuePair<string,string>>?
. 我的map
变量是Dictionary<string,string>
,大概会实现接口IEnumerable<KeyValuePair<string,string>>
所以我希望这里没有问题。 那么为什么会出现警告呢?
我正在使用面向 NETCore5 的 Visual Studio 16.8.0。
我觉得这里实际上有几个问题:
后者更容易回答。 前者,没有那么多(你得问微软)。
我还没有安装 .NET 5。 但是我可以使用以下代码在 .NET Core 3.1 中重现完全相同的警告:
static void Main(string[] args)
{
var map = new Dictionary<string, string>()
{
["hello"] = "world"
};
M1(map);
}
static void M1(IEnumerable<KeyValuePair<string?, string?>>? nameValueCollection) { }
可以从您复制/粘贴到问题中的警告中推断出FormUrlEncodedContent
类构造函数的 .NET 5 版本的实际声明必须是什么,所以我只写了一个不需要安装 .NET 5 的示例。
事实上,如果我们查看源代码,我们可以看到该构造函数的实际声明:
public FormUrlEncodedContent(IEnumerable<KeyValuePair<string?, string?>> nameValueCollection)
这告诉我文档是不准确的,如果不是完全不正确的话。 我没有关注支持可为空引用类型的框架的更改,所以我不确定文档是否会显示这种情况下的可为空类型参数。 在我看来它应该,但显然不是。 至少,令我惊讶的是,文档将nameValueCollection
参数本身显示为可空(实际上并非如此),更不用说它显示KeyValuePair<TKey, TValue>
类型参数为不可空,而实际上它们可以为空。
但是无论如何,通过了解 .NET 5 中的实际方法签名是什么,我们可以说警告存在,因为确实,您的调用涉及传递与类型参数的可为空性不一致的类型。
为什么这会产生警告? 嗯……虽然我们知道在这种情况下没有代码将写入可枚举值中的可为空值,但没有编译时保证。 由于您的对象正在返回值是不可为空的引用的对象,并且您将其传递给一个声明为接受可为空值的方法,这意味着理论上,该方法可以通过将这些可空值设置为null
来修改这些可空值, 违反了您自己的代码假设值不可为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.