I was thinking of keeping three pieces of information for each object of a list. So I can create a class with three properties for those three pieces of information, and then create a Collection of that class type... But I was wondering in .NET 3.5 ( and not 4.0 ) have any thing built in for that? for example a dictionary...that one keep two pieces of information for each item, key and value...but I need three. Do we have anything built-in for that?
Create your own Triple class:
public class Triple<T,X,Y>
{
public T t{get;set;}
public X x{get;set;}
public Y y{get;set;}
}
It's a clear approach and more manageable than directly using a dictionary, like: Dictionary<Key, KeyValuePair<X,Y>>
and other approaches like this.
You can create your own Tuple<T1, T2, T3>
:
public class Tuple<T1, T2, T3> : IEquatable<Object>{
public T1 Item1{
get;
set;
}
public T2 Item2{
get;
set;
}
public T3 Item3{
get;
set;
}
public Tuple(T1 Item1, T2 Item2, T3 Item3){
this.Item1 = Item1;
this.Item2 = Item2;
this.Item3 = Item3;
}
public override bool Equals( object obj ) {
if ( obj == null || (obj as Tuple<T1, T2, T3>) == null ) //if the object is null or the cast fails
return false;
else {
Tuple<T1,T2,T3> tuple = ( Tuple<T1, T2, T3> ) obj;
return Item1.Equals( tuple.Item1 ) && Item2.Equals(tuple.Item2) && Item3.Equals(tuple.Item3);
}
}
public override int GetHashCode( ) {
return Item1.GetHashCode( ) ^ Item2.GetHashCode() ^ Item3.GetHashCode();
}
public static bool operator == ( Tuple<T1, T2, T3> tuple1, Tuple<T1, T2, T3> tuple2 ) {
return tuple1.Equals( tuple2 );
}
public static bool operator != ( Tuple<T1, T2, T3> tuple1, Tuple<T1, T2, T3> tuple2 ) {
return !tuple1.Equals( tuple2 );
}
}
The solution of creating a generic tuple class similar to the one that's added in .Net 4 is explained in other answers, but I do feel it's important to state that it might be better to create a small class to semantically represent the data you're storing with meaningful properties. A generic 3-tuple might work now, but you might want to expand on the functionality later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With