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.
Any ideas?
private List<KeyValuePair<String,String>> formData = new List<KeyValuePair<String, String>>[]
{
new KeyValuePair<String, String>("lsd",""),
new KeyValuePair<String, String>("charset", "")
};
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);
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 Tuple
class:
pirvate List<Tuple<string, string>> formData = new List<Tuple<string, string>>()
{
new Tuple<string, string>("lsd",""),
new Tuple<string, string>("charset", "")
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With