Arkady
Arkady

Reputation: 393

Add to Dictionary<string, string> using foreach

what is the best way to ensure that optionValue is always unique in the following scenario?

public Dictionary<string, string> Dict = new Dictionary<string, string>();

    foreach (string optionKey in i.options.Keys)
    {
       string optionValue = i.options.Values.ToString();
       Dict.Add(optionKey, optionValue);
    }

EDIT: i.options contains a key and a value pair. I need to ensure that for each key the corresponding value is added to the dictionary

EDIT2: corrected order of Dict.Add(optionKey, optionValue)

Upvotes: 1

Views: 8558

Answers (7)

BlueM
BlueM

Reputation: 6888

In a Dictionary all keys are unique. If you want to prevent an exception when adding entries to the Dictionary, use

if (!Dict.ContainsKey(optionKey)) {
  Dict.Add(optionKey, optionValue);
} else {
  Debug.Print("Key '"+optionKey+"' already exists");
}

Upvotes: 1

Matt T
Matt T

Reputation: 511

Other folks have been suggesting that you simply test to see if the key exists, and skip adding if it does. However, this may lead to lost data. You should create a custom class or type and use that with your dictionary like so:

public Dictionary< Guid, CustomObjectOrType > Dict = new Dictionary< Guid, CustomObjectOrType >();

This way you can ensure that each key is unique (a Guid) while preserving all option values and option keys.

Upvotes: 0

Simon Wang
Simon Wang

Reputation: 2943

If you are just worried about exceptions, then just use the item property:

Dict[optionValue] = optionKey;

MSDN: If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.

Or if you really want to do the check, use the ContainsKey method to check

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Upvotes: 0

Alberto De Caro
Alberto De Caro

Reputation: 5213

public Dictionary<string, string> Dict = new Dictionary<string, string>();

foreach (string optionKey in i.options.Keys)
{
   string optionValue = i.options.Values.ToString();
   if(!Dict.ContainsValue(optionValue))
       Dict.Add(optionValue, optionKey);
}

See http://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.100).aspx

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124716

Depends how you want to handle duplicates.

E.g "last one wins"

foreach(...)
{
    // Will overwrite an existing key
    Dict[optionValue] = optionKey;
}

"First one wins":

foreach(...)
{
    if (!Dict.ContainsKey(optionValue)) Dict.Add(optionValue, optionKey);
}

Upvotes: 0

Bryan Crosby
Bryan Crosby

Reputation: 6554

A cursory browsing of the MSDN documentation reveals that you can use the obvious method ContainsKey(string) to check to see if a dictionary contains a certain key.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160912

Just test for it:

if(!Dict.ContainsKey(optionValue))
   Dict.Add(optionValue, optionKey)

Based on your variable names it looks like you have key and value reversed though, if optionKey is your lookup key it should be:

Dict.Add(optionKey, optionValue);

Upvotes: 2

Related Questions