namco
namco

Reputation: 6338

how to use this definition in c# with dictionaries

GROUP["10"]["MATH"] = 30;
GROUP["11"]["MATH"] = 40;
GROUP["9"]["CHEM"] = 50;
...

Can u tell me how to use it with dictionaries. How to define a dictionary for this example?

Upvotes: 0

Views: 131

Answers (8)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

You could also create your own dictionary:

public class GroupDict
{
    const string Separator = ";";

    Dictionary<string, int> _internalDict = new Dictionary<string, int>();

    public void Add(string key1, string key2, int value)
    {
        _internalDict.Add(key1 + Separator + key2, value);
    }

    public int this[string key1, string key2]
    {
        get { return _internalDict[key1 + Separator + key2]; }
        set { _internalDict[key1 + Separator + key2] = value; }
    }

    public int Count
    {
        get { return _internalDict.Count; }
    }
}

You can then use it like:

GroupDict groups = new GroupDict();
groups.Add("10", "MATH", 30);
// OR
groups["10", "MATH"] = 30;

Console.WriteLine(groups["10", "MATH"]);

Upvotes: 1

TheJoeIaut
TheJoeIaut

Reputation: 1532

Also note that you can't create a Multi-Key Dictionary in C#.

You will need your own struct for that.

Upvotes: 0

Jorge Ferreira
Jorge Ferreira

Reputation: 97849

Option 1: Use a Dictionary<string, Dictionary<string, int>>.

Dictionary<string, int> group10 = new Dictionary<string, int>();
group10.Add("MATH", 30);
Dictionary<string, int> group11 = new Dictionary<string, int>();
group10.Add("MATH", 40);
Dictionary<string, int> group9 = new Dictionary<string, int>();
group10.Add("CHEM", 50);
var group = new Dictionary<string, Dictionary<string, int>>();
group.Add("10", group10);
group.Add("11", group11);
group.Add("9", group9);

int v1 = group["10"]["MATH"]; // v1 = 30
int v2 = group["11"]["MATH"]; // v2 = 40
int v3 = group["9"]["CHEM"]; // v3 = 50

Option 2: Or create a compound key for the dictionary that overrides GetHashCode and Equals, or you could use Tuple.

var group = new Dictionary<Tuple<string, string>, int>();
group.Add(Tuple.Create("10", "MATH"), 30);
group.Add(Tuple.Create("11", "MATH"), 40);
group.Add(Tuple.Create("9", "CHEM"), 50);

int v1 = group[Tuple.Create("10", "MATH")]; // v1 = 30
int v2 = group[Tuple.Create("11", "MATH")]; // v2 = 40
int v3 = group[Tuple.Create("9", "CHEM")]; // v3 = 50

If using Option 1 you must remember to create the second level Dictionary<string, int>, before adding the integer values.

Upvotes: 2

Something like this may be

Dictionary<string, Dictionary<string><int>> dict = new Dictionary<string, Dictionary<string><int>>(); 

Upvotes: 1

Russ Cam
Russ Cam

Reputation: 125498

You could create a Dictionary<Tuple<string, string>, int> but you may better defining a class/ custom collection to better represent this, it depends on what your use case is.

An example of Dictionary<Tuple<string, string>, int>

var dict = new Dictionary<Tuple<string, string>, int>
           {
               { new Tuple<string,string>("10", "MATH"), 30 },
               { new Tuple<string,string>("11", "MATH"), 40 },
               { new Tuple<string,string>("9", "CHEM"), 50 }
           };

Upvotes: 4

Glory Raj
Glory Raj

Reputation: 17701

use this one ......

 Dictionary<string,Dictionary<string,int>>  

pls go through this link for more info ....On dictionaries,if you are new to dictionaries..

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Upvotes: 4

Dennis
Dennis

Reputation: 2142

Dictionary<string, Dictionary<string, int>>

Use:

Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>();
Dictionary<string, int> inner = new Dictionary<string, int>();
inner.Add("MATH", 30);
dic.Add("10", inner);

...

Access

dic["10"]["MATH"]

Upvotes: 4

Yochai Timmer
Yochai Timmer

Reputation: 49251

Dictionary<string,Dictionary<string,int>>

The first [] returns a dictionary, and the second [] operates on that dictionary and returns an int

Upvotes: 5

Related Questions