Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we have some sort of a Triple collection in C#

Tags:

c#

collections

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?

like image 695
Bohn Avatar asked Aug 18 '12 23:08

Bohn


3 Answers

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.

like image 193
Saeed Amiri Avatar answered Oct 15 '22 22:10

Saeed Amiri


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 );
    }
}
like image 44
Omar Avatar answered Oct 16 '22 00:10

Omar


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.

like image 3
jwrush Avatar answered Oct 16 '22 00:10

jwrush