79

I have defined a class myComplex. I need to map it to integers. In C++ I would have created a map as map<myComplex,int> first;

How to do such thing in C#?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Abhash Kumar Singh
  • 1,157
  • 2
  • 12
  • 22

4 Answers4

134

The equivalent would be class SortedDictionary<TKey, TValue> in the System.Collections.Generic namespace.

If you don't care about the order the class Dictionary<TKey, TValue> in the System.Collections.Generic namespace would probably be sufficient.

dalle
  • 18,057
  • 5
  • 57
  • 81
  • 3
    If they don't care about order, `Dictionary` is more than sufficient, it's preferable. – Daniel Apr 19 '18 at 16:28
  • 9
    @Daniel: Yes, but that begs the question: why did they use 'map' in C++ if they didn't care about order? They could have used unordered_map in C++ (assuming they've been awake since C++11). – Dave Doknjas Apr 23 '18 at 13:57
  • @DaveDoknjas Agreed. My point is just that your statement was that given they don't care about order, `Dictionary` is "probably [...] sufficient". Actually, in the case where they don't care about order, `Dictionary` is more than sufficient; it's preferable. – Daniel Apr 24 '18 at 14:33
61

std::map<Key, Value>SortedDictionary<TKey, TValue>

std::unordered_map<Key, Value>Dictionary<TKey, TValue>

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
hansmaad
  • 18,417
  • 9
  • 53
  • 94
14

Take a look at the Dictionary class in System::Collections::Generic.

Dictionary<myComplex, int> myMap = new Dictionary<myComplex, int>();
1

.NET Framework provides many collection classes too. You can use Dictionary in C#. Please find the below msdn link for details and samples http://msdn.microsoft.com/en-us/library/xfhwa508.aspx