Mr. JavaScript Mr. JavaScript - 3 years ago 98
Javascript Question

.NET Dictionary vs Class properties

Coming from a web development background (javascript), collections like dictionaries are often implemented as objects because their properties can be referenced by association:

// Javascript example
var myDictionary = {
namevalue1 : "value1",
namevalue2 : "value2"

console.log( myDictionary.namevalue1 ); // "value1"
console.log( myDictionary["namevalue1"] ); // "value1"

I'm writing C# now and I'd like to know if the proper way to store string values for lookup is in a singleton class or in a generic dictionary. I like that classes are strongly typed and give intellisense support, but I'm anticipating a lot of changes and maintaining class properties vs. simply calling Dictionary methods seems like a headache. Are the performance benefits so great that I should choose one over the other?

Answer Source

In fact, a class would be several orders of magnitude faster than a Dictionary. If you feel it's more convenient, that's even better :) So if you're able to make classes for it, do so. If the objects you represent really should be classes (i.e. are fundamentally objects and not key/value pairs) then all the more reason.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download