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 :
- collection.IconPath is null ?
- it throw an exception ?
According to my quick test, adding a null key to a dictionary causes an
ArgumentNullExceptionand trying to retrieve a key that doesn’t exist results in aKeyNotFoundException.As for code reviewing how best to write it, try https://codereview.stackexchange.com/
Alternative methods include
TryGetValueandContainsKey.