Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Dictionary<string, string> object in some method

Tags:

c#

dictionary

Can somebody give an example of how i pass Dictionary object in some method.???

like image 229
SPBeginer Avatar asked Jan 19 '11 10:01

SPBeginer


2 Answers

You can pass a dictionary like a normal argument:

private void MyMethod(Dictionary<string,string> myDictionary) { 
    //code 
}

Or you can pass it as an object and cast later like:

private void MyMethod(Object myDictionary) { 
   string color = ((Dictionary<string,string>)myDictionary)["color"];
}
like image 181
deadlock Avatar answered Nov 15 '22 21:11

deadlock


If from the syntax you are referring to .NET then it would be as simple as passing any other parameter into a method

AMethod(Dictionary<string,string> dictionary)
{
//  Stuff
}

If you are doing a new inside the AMethod to create a new dictionary then don't forget to add a "ref".

like image 42
Paul Hadfield Avatar answered Nov 15 '22 20:11

Paul Hadfield