2

presaying i'm still a newbie. I've this situation to solve. I know it's something very very easy. But I just started to study C# world.

I need to pass to a function an associative array of x elements.

I need to pass some data like data1=een&data2=twee&data3=drie&data4=fier.

Then my questions are:

1) how can i declare my function?

private void myFoo(List<List<string>> data){}
private void myFoo(string[] data){}

2) how can I loop on a list of a string list? something like this -i know it's not correct-

3) how can I create my array?

2
  • Will your value for x be set, or does it vary? Commented Sep 5, 2013 at 15:43
  • @Paddy i did not understand what you mean Commented Sep 5, 2013 at 15:50

1 Answer 1

11

One option would be to use a Dictionary<string,string>. This would allow you to have keyed values passed through your functions.

private void myFoo(Dictionary<string,string> data)
{}

Build it as:

var dict = new Dictionary<string,string>();
dict.Add("data1", "een");
dict.Add("data2", "twee");

Fetch via:

var value = dict["data1"];  // You can also use TryGetValue, etc.
Sign up to request clarification or add additional context in comments.

7 Comments

I was about to propose the same.
System.Collections.Specialized.StringDictionary would be more appropriate is what I feel
@SriramSakthivel That class isn't more appropriate - it was a better option before generics (if you're using .NET 1.1), but post-generics, those classes are really ones that are better unused.
part 2) of OP's question - msdn.microsoft.com/en-us/library/5tbh8a42.aspx shows you how you loop on the dictionary using 'foreach' under 'Remarks'. You will get a KeyValue Pair where Key = data1 and Value = een
@SriramSakthivel Why? It has less features than a Dictionary. It's only purpose was to have a strongly typed HashTable back before generics existed. Once .NET 2.0 came out it really only needed to exist for backward compatibility. There is no reason to ever use it in new development at this point.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.