What is C# equivalent of <map> in C++?

197,512

Solution 1

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.

Solution 2

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

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

Solution 3

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

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

Solution 4

.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

Share:
197,512
Abhash Kumar Singh
Author by

Abhash Kumar Singh

Updated on July 05, 2022

Comments

  • Abhash Kumar Singh
    Abhash Kumar Singh almost 2 years

    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#?

  • Daniel
    Daniel about 6 years
    If they don't care about order, Dictionary is more than sufficient, it's preferable.
  • Dave Doknjas
    Dave Doknjas about 6 years
    @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).
  • Daniel
    Daniel about 6 years
    @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.