Click to See Complete Forum and Search --> : couldn't understand syntax


visitkppradeep@yahoo.com
February 28th, 2008, 12:09 AM
public static Dictionary<string, double> m_BankBalance = new Dictionary<string, double>();

what does this syntax mean?

riscutiavlad
February 28th, 2008, 01:29 AM
The above declares and instantiates a dictionary object with string keys and double values, named m_BankBalance.

The Dictionary class uses generics, meaning that you can provide any types between < >. The first type represents the keys and the second the values. A Dictionary behaves like a hash table, having O(n) = 1 to lookup entries (values), meaning it has the fastest item lookup. Keys must be unique, so you can't add two values with the same key.

The first part is the declaration:

public static Dictionary<string, double> m_BankBalance

The second part is the instantiation, which can also be done in code (in fact, the compiler will move the statement inside the constructor). To instantiate a new Dictionary object, the following statement is used:

m_BankBalance = new Dictionary<string, double>();

This is a call to the default constructor (which takes no parameters), but the generic types have to be specified.