gpt4 book ai didi

C# new Dictionary() is IDictionary 返回 false

转载 作者:行者123 更新时间:2023-11-30 19:35:35 25 4
gpt4 key购买 nike

例子:

void Main()
{
var dic = new Dictionary<string, object>();

Console.WriteLine(dic is IDictionary<object, object>); //False
Console.WriteLine(dic is IDictionary<dynamic, dynamic>); //False
Console.WriteLine(dic is IDictionary<string, object>); //True
}

string 不也是一个对象吗?为什么 dic is IDictionary<object, object>得到假。

最佳答案

String派生自 Object .但是IDictionary<string, object>不源自 IDictionary<object, object> .这就是它返回 false 的原因。您可能想阅读 .NET 中的协变和逆变概念 https://learn.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance

关于C# new Dictionary<string, object>() is IDictionary<object, object> 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54267355/

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