Equivalent of Python Dictionary

  • Thread starter Thread starter bg_ie
  • Start date Start date
B

bg_ie

Hi,

I have my own class and I'd like to save objects of this class in a
dictionary. In python this works something along the lines of:

a = MyObject();
b = MyObject();
c = MyObject();

myObjectDict = {}
myObjectDict['a'] = a
myObjectDict['b'] = b
myObjectDict['c'] = c

How do I do this in C#?

Thanks,

Barry
 
(assumes 2.0, and note that I'm interpreting 'a' as a string not a
char - hope this is correct ;-p)

using System.Collections.Generic;
....
MyObject a = new MyObject(), b = new MyObject(), c = new MyObject();
Dictionary<string, MyObject> dict = new Dictionary<string,
MyObject>();
dict["a"] = a;
dict["b"] = b;
dict["c"] = c;

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top