5

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", "") }; 
1
  • At a guess, and without knowing the error, it's the [] pair at the end of the first line (it could also be the ; char after the first KeyValuePair, it should be a ,. Can you provide the error you're getting, save us copying and pasting your code? Commented Feb 27, 2013 at 10:32

6 Answers 6

13

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

I haven't used C# - or programmed - for over a year, so was going by what I remembered. But certainly is a better solution.
What I have to do in case where my data have same values?
I suggest you to post a new question. You will find more help than asking for a solution through a comment on a 5 year old question.
3

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", "") }; 

Comments

0

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.

Comments

0

remove the [] from the declaration

Comments

0
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: ,.

Comments

0

try

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.