Guillaume Slashy
Guillaume Slashy

Reputation: 3624

About accessing the Value of a Dictionary

This is a noob question, I know but I didn't find the answer on the internet...

I got the following Dictionary :

private static Dictionary<Qb.Solution.QbDataStore.DataStoreType, string> DataStoreType2IconPath = new Dictionary<QbDataStore.DataStoreType, string>()
{
    { Qb.Solution.QbDataStore.DataStoreType.CommonData, "Common" },
    { Qb.Solution.QbDataStore.DataStoreType.ApplicationData, "Application" },
    { Qb.Solution.QbDataStore.DataStoreType.FrameData, "Frame" }
};

and then I have a method with a Qb.Solution.QbDataStore.DataStoreType dataType parameter and I got this :

if (dataType != null)
{
    collection.IconPath = DataStoreType2IconPath[dataType];
}

My question is :

If I remove the if test, and that I don't know how (theorytically impossible) dataType is null :

  1. collection.IconPath is null ?
  2. it throw an exception ?

Upvotes: 0

Views: 132

Answers (4)

Richard
Richard

Reputation: 1142

As mentioned already, you cannot add a null key to a Dictionary - http://msdn.microsoft.com/en-us/library/k7z0zy8k.aspx

You might want to check if the key exists using the ContainsKey() method rather than use TryGetValue or catching an exception. (You'll still need an if statement with TryGetValue, and best not to program using exceptions for logic if you can help it.)

if (DataStoreType2IconPath.ContainsKey(dataType))
{
    collection.IconPath = DataStoreType2IconPath[dataType];
}

If you really want to avoid the if statement, you could use the conditional operator:

collection.IconPath = DataStoreType2IconPath.ContainsKey(dataType) ?
                        DataStoreType2IconPath[dataType] : null;

Personally I prefer this "expression" style of programming rather than statements, but that's just me. :-)

Upvotes: 1

Adam Houldsworth
Adam Houldsworth

Reputation: 64487

According to my quick test, adding a null key to a dictionary causes an ArgumentNullException and trying to retrieve a key that doesn't exist results in a KeyNotFoundException.

namespace ConsoleApplication46
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var d = new Dictionary<string, int>();

            d.Add(null, 1);

            Console.WriteLine(d["adam"]);
            Console.Read();
        }
    }
}

As for code reviewing how best to write it, try https://codereview.stackexchange.com/

Alternative methods include TryGetValue and ContainsKey.

Upvotes: 2

Phil-R
Phil-R

Reputation: 2253

It will throw an exception, so you're better with the if test.

You can also do it with the TryGetValue : http://msdn.microsoft.com/fr-fr/library/bb347013.aspx

Upvotes: 3

ColinE
ColinE

Reputation: 70142

If you try to retrieve an entry from a dictionary which is not present a KeyNotFoundException is thrown, see the MSDN documentation. If this is not your desired behaviour use the TryGetValue method instead.

Upvotes: 2

Related Questions