相关疑难解决方法(0)

IDictionary <string,string>或NameValueCollection

我有一个场景,我可以使用NameValueCollection或IDictionary.但我想知道哪一个会更好地表现.

- 使用NameValueCollection

NameValueCollection options()
{
    NameValueCollection nc = new NameValueCollection();

    nc = ....; //populate nc here

    if(sorting)
       //sort NameValueCollection nc here

    return nc;
}
Run Code Online (Sandbox Code Playgroud)

- 使用IDictionary

IDictionary<string, string> options()
{
    Dictionary<string, string> optionDictionary = new Dictionary<string, string>();

    optionDictionary = ....; //populate

    if(sorting)
       return new SortedDictionary<string, string>(optionDictionary);
    else
       return optionDictionary;
}
Run Code Online (Sandbox Code Playgroud)

c# generics

31
推荐指数
3
解决办法
1万
查看次数

c#custom Dictionary <string,string>,它接受用于序列化的重复键

我需要实现一个类似于Dictionary的自定义功能,但可以插入重复的键.所以基本上我需要从Dictionary中将对象序列化为以下JSON:

{
  "One":"Value 1",
  "Two":"Value x",
  "One":"Value 10",
  "Two":"Value 100"
} 
Run Code Online (Sandbox Code Playgroud)

如上所示,我有重复的密钥......

有什么建议?整点是上面格式的JSON输出

编辑:

KeyValuePair<string,string> 不起作用!

这是结果:

[{"Key":"One","Value":"Two"},{"Key":"One","Value":"Two"}]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,序列化为JSON将使Key和Value关键字变为空白,其中Dictionary将使用实际键值替换Key,并使用提供的值替换值.

c# json dictionary

1
推荐指数
1
解决办法
1121
查看次数

标签 统计

c# ×2

dictionary ×1

generics ×1

json ×1