Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# check if key exists in dictionary then pass on its value

Tags:

c#

dictionary

In my desktop C# application I start with a dictionary. I want to be able to check this dictionary for a key. If the dictionary has this key, I would like to pass it on to a method. If the dictionary doesn't have this key, I would like to create a blank list and just pass that on instead. How can I do this?

I get the error "given key was not present in the dictionary". Can I add a default so it is never null maybe?

// myDic was declared as a Dictionary<string, List<string>    

// Here is how I call someFunction
string text = SomeFunction(stringValue1, stringValue2, myDic[field1.field2]);

// SomeFunction looks like this
string SomeFunction (string string1, string string2, List<string> ra) 
{  
     // method 
     return stringResult;
} 
like image 810
cd300 Avatar asked Apr 23 '15 14:04

cd300


4 Answers

What you need to do is make sure the dictionary actually contains the given key in the dictionary.

If you need to extract the value by key, use TryGetValue method:

string value;
if (myDict.TryGetValue(key, out value))
{
    // Key exists in the dictionary, do something with value.
}
like image 118
Yuval Itzchakov Avatar answered Nov 18 '22 14:11

Yuval Itzchakov


Updated based on comments. To pass one key that may or may not exist you may do this(assuming the value is a List):

// assuming the method we are calling is defined like this:
// public String SomeFunction(string string1, String string2, List<String> ra)  

List<string> valueToPassOn;
if (_ra.ContainsKey(lc.Lc))
{
     valueToPassOn = _ra[lc.Lc]
}
else
{
     valueToPassOn = new List<string>(); 
}

string text = tooltip.SomeFunction(something1, something2, valueToPassOn); 

Should you want to pass an entire dictionary (as the question originally read), regardless of whether or not the dictionary exists:

You have two options. Either create the dictionary regardless like this:

if (myDic == null)
{
     // change var and var2 to the types of variable they should be, ex:
     myDic = new Dictionary<string, List<string>>();
}
string text = SomeFunction(stringValue1, stringValue2, myDic);

or, what is probably the better option, in the declaration of the function SomeFunction add a dictionary as a variable with a default parameter. Just be sure that your function knows what to do if the dictionary is null.

string SomeFunction(string string1, string string2, Dictionary dictionary = null)
{
     // method here
}
like image 22
bkribbs Avatar answered Nov 18 '22 14:11

bkribbs


You can check if the key exists using ContainsKey method and if it returns false you can pass a default value you want:

// replace default(string) with the value you want to pass
// if the key doesn't exist
var value = myDic.ContainsKey(field1.field2) ? myDic[field1.field2] : default(string);
string text = SomeFunction(stringValue1, stringValue2, value);
like image 12
Selman Genç Avatar answered Nov 18 '22 15:11

Selman Genç


Use one of the following snippets in order to check if dictionary is empty and take some action:

var x = new Dictionary<string, string>();

if (x.Any())
{
    //....
}

if (x.ContainsKey("my key"))
{

}

if (x.ContainsValue("my value"))
{

}

if (x.Count > 0)
{

}
like image 2
Pawel Rosinski Avatar answered Nov 18 '22 14:11

Pawel Rosinski