C# 设置键/值对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15109757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:05:10  来源:igfitidea点击:

Setting Key/Value Pairs

c#

提问by James Jeffery

I've created a List as a property of the class, and want to set the Key/Value pairs when defining the List. I was originally using a structure but realized it's probably not the ideal solution so I changed it to a List. The problem is I'm getting an error with the syntax.

我已经创建了一个列表作为类的属性,并希望在定义列表时设置键/值对。我最初使用的是一个结构,但意识到它可能不是理想的解决方案,所以我将其更改为 List。问题是我遇到了语法错误。

Any ideas?

有任何想法吗?

private List<KeyValuePair<String,String>> formData = new List<KeyValuePair<String, String>>[]
    {
            new KeyValuePair<String, String>("lsd",""),
            new KeyValuePair<String, String>("charset", "")
    };

采纳答案by Steve

Probably I'm missing something, but I would have used a Dictionary instead of
So simple....

可能我遗漏了一些东西,但我会使用字典而不是
那么简单......

Dictionary<string, string>formData = new Dictionary<string, string>
{
    {"lsd", "first"},
    {"charset", "second"}
};    

and then use it in these ways:

然后以这些方式使用它:

foreach(KeyValuePair<string, string>k in formData)
{
    Console.WriteLine(k.Key);
    Console.WriteLine(k.Value);
}
....
if(formData.ContainsKey("lsd"))
    Console.WriteLine("lsd is already in");
....    
string v = formData["lsd"];
Console.WriteLine(v);

回答by Colin Mackay

Change the semi-colon to a comma on the third line and remove the square brackets from the first line.

将第三行的分号更改为逗号并删除第一行的方括号。

private List<KeyValuePair<String,String>> formData = new List<KeyValuePair<String, String>>
{
        new KeyValuePair<String, String>("lsd",""),
        new KeyValuePair<String, String>("charset", "")
};

Incidentally, if you change it to a Dictionary you get the ability to look up the values by their key more easily.

顺便说一句,如果您将其更改为字典,您将能够更轻松地通过键查找值。

回答by CloudyMarble

remove the []from the declaration

[]从声明中删除

回答by MarcinJuraszek

private List<KeyValuePair<String,String>> formData = new List<KeyValuePair<String, String>>()
    {
            new KeyValuePair<String, String>("lsd",""),
            new KeyValuePair<String, String>("charset", "")
    };
  1. Why []after the constructor?
  2. Items within collection initializer has to be separated using comma: ,.
  1. 为什么[]在构造函数之后?
  2. 集合初始值设定项中的项目必须使用逗号分隔:,

回答by Bj?rn M?rtensson

try

尝试

           private List<KeyValuePair<String, String>> formData = new List<KeyValuePair<String, String>>
    {
            new KeyValuePair<String, String>("lsd",""),
            new KeyValuePair<String, String>("charset", "")
    };

回答by Mohammad Dehghan

Try this:

尝试这个:

private List<KeyValuePair<String,String>> formData = new List<KeyValuePair<String, String>>
{
    new KeyValuePair<String, String>("lsd",""),
    new KeyValuePair<String, String>("charset", "")
};

You had an extra []in your definition. You are not creating an array, so you don't need it. Also when initializing list with some values, the values should be separated by a comma (,).

[]的定义中有一个额外的。您没有创建数组,因此不需要它。同样,在使用某些值初始化列表时,这些值应以逗号 ( ,)分隔。

In my opinion, a better approach would be to use Tupleclass:

在我看来,更好的方法是使用Tupleclass

pirvate List<Tuple<string, string>> formData = new List<Tuple<string, string>>()
{
    new Tuple<string, string>("lsd",""),
    new Tuple<string, string>("charset", "")
};