C#2.0 Dictionary<Key,Value> indexer operation is strange?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi ,

I use VS 2005 beta1 to do some thing as follows.

declare a Dictionary<ulong,DelegateTyep> dictionary = new ...

and use dictionary object such as

dictionary [aUlongNumber] += aDelegateTyepObject;

then compiler is ok ,but throw KeyNotFoundException at run-time.
OK,I think just a missed initailization,thne add it like

dictionary[aUlongNumber] = null;

It becomes to be vary strange.It doesn't throw exception any more.

What's difference between '=' and '+=' on Dictionary's indexer operation?
Them are also assigning value to indexing entry,right?
Or some thiing compiler do for '+=' operator(Delegate.Combine)?

Thanks for your help
 
What's difference between '=' and '+=' on Dictionary's indexer operation?

The same as always; += expands to

dictionary[aUlongNumber] = dictionary[aUlongNumber] +
aDelegateTypeObject;

and retrieving dictionary[aUlongNumber] before you have added that key
to the dictionary will throw an exception.



Mattias
 
Back
Top