What would the c# equivalent of make_pair be
-
In C++\STL i can create pairs of values using make_pair so I could, for example, look up a value in a map/dictionary using a 2 value key:
XXX::iterator it = m_map.find(make_pair(x,y));
Is there an equivalent in C# or if not a way around it? TIAA
Hashtable
stores key/values pairs that are bothobject
s, meaning that you can store anything (since every Type in .NET extendsSystem.Object
). This means that you can store, for example, a class (best to use reference types to avoid boxing and unboxing performance penalties) with a couple of properties like so:public class Person
{
string name, DateTime birthday;
public string Name
{
get { return name; }
set { if (value == null) throw new ArgumentNullException(); name = value; }
}
public DateTime Birthday
{
get { return birthday; }
set { birthday = value; }
}
}Then create an
IComparer
implementation to compare whatever property or properties you want, or have your class implementIComparable
so that the defaultComparer
can juse use your implementation (this option is often best when defining your own types as it works without implementing and using a separate Type). If you read the documentation for theHashtable
, however, you'll notice that if you don't pass anIComparer
to the constructor the default comparer it uses will use theEquals
override. So, you could also overrideObject.Equals
(which implies you should overrideObject.GetHashCode
) and not pass a comparer, or implementIComparable
on your class and passComparer.Default
to theHashtable
constructor, or implementIComparer
and pass your implementation to theHashtable
constructor. This will allow you to compare keys, not values, though. For that, enumerate theHashtable.Values
collection or enumerate (using theIDictionaryEnumerator
) the entireHashtable
to compare values so that you have both the key and value. As far as whether to overrideEquals
or implementIComparable
, you could do both. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
In C++\STL i can create pairs of values using make_pair so I could, for example, look up a value in a map/dictionary using a 2 value key:
XXX::iterator it = m_map.find(make_pair(x,y));
Is there an equivalent in C# or if not a way around it? TIAYou might look into classes that implement
IDictionary
, such as HybridDictionary[^]. The method access will be different but you will find what you need for version 1.1 of the framework to implement it. With version 2.0 of the .NET Framework you can do something as simple as the following with generics:public class pair<T1, T2> { public T1 First { get {return this.t1;} set {this.t1 = value;} } public T2 Second { get { return this.t2; } set { this.t2 = value; } } public pair(T1 type1, T2 type2) { this.t1 = type1; this.t2 = type2; } private T1 t1; private T2 t2; } /// Implementation private void button1\_Click(object sender, EventArgs e) { pair<int, int> i = new pair<int, int>(5, 10); MessageBox.Show(string.Format("First value:{0}, second value:{1}", i.First.ToString(), i.Second.ToString())); }
- Nick Parker
My Blog | My Articles -
A
Hashtable
stores key/values pairs that are bothobject
s, meaning that you can store anything (since every Type in .NET extendsSystem.Object
). This means that you can store, for example, a class (best to use reference types to avoid boxing and unboxing performance penalties) with a couple of properties like so:public class Person
{
string name, DateTime birthday;
public string Name
{
get { return name; }
set { if (value == null) throw new ArgumentNullException(); name = value; }
}
public DateTime Birthday
{
get { return birthday; }
set { birthday = value; }
}
}Then create an
IComparer
implementation to compare whatever property or properties you want, or have your class implementIComparable
so that the defaultComparer
can juse use your implementation (this option is often best when defining your own types as it works without implementing and using a separate Type). If you read the documentation for theHashtable
, however, you'll notice that if you don't pass anIComparer
to the constructor the default comparer it uses will use theEquals
override. So, you could also overrideObject.Equals
(which implies you should overrideObject.GetHashCode
) and not pass a comparer, or implementIComparable
on your class and passComparer.Default
to theHashtable
constructor, or implementIComparer
and pass your implementation to theHashtable
constructor. This will allow you to compare keys, not values, though. For that, enumerate theHashtable.Values
collection or enumerate (using theIDictionaryEnumerator
) the entireHashtable
to compare values so that you have both the key and value. As far as whether to overrideEquals
or implementIComparable
, you could do both. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
You might look into classes that implement
IDictionary
, such as HybridDictionary[^]. The method access will be different but you will find what you need for version 1.1 of the framework to implement it. With version 2.0 of the .NET Framework you can do something as simple as the following with generics:public class pair<T1, T2> { public T1 First { get {return this.t1;} set {this.t1 = value;} } public T2 Second { get { return this.t2; } set { this.t2 = value; } } public pair(T1 type1, T2 type2) { this.t1 = type1; this.t2 = type2; } private T1 t1; private T2 t2; } /// Implementation private void button1\_Click(object sender, EventArgs e) { pair<int, int> i = new pair<int, int>(5, 10); MessageBox.Show(string.Format("First value:{0}, second value:{1}", i.First.ToString(), i.Second.ToString())); }
- Nick Parker
My Blog | My ArticlesYou will have to use a struct or override GetHashCode()/Equals() else it wont work for key of a hashtable :p top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
You will have to use a struct or override GetHashCode()/Equals() else it wont work for key of a hashtable :p top secret
Download xacc-ide 0.0.3 now!
See some screenshotsIt works much better when you use generics though. ;-) - Nick Parker
My Blog | My Articles