This project is read-only.
Project Description
System.Tuples is a small tuple library. It uses T4 to generate tuples, and is made to be compatible with .net 2.0, .net 3.0 and .net 3.5.

The new .net 4.0 framework brings with it a new System.Tuple heterogeneous, ordered and type-safe container type. However, for those of us that are stuck with an older version of the framework this won't be of much use. That is why I have decided to publish a small Tuple library I have created a couple of months ago.

The library uses T4 templates to generate tuple classes up to a certain length (that can be changed). The templates have been made so that is is very easy to remove features that are incompatible with previous versions, just by setting a few flags and regenerating and recompiling the code.

The library is now also available over the Nuget gallery under the name System.Tuples.

And now, some code examples :

To create a tuple either use a constructor of the Tuple.New method (which brings with it automatic parameter type deduction):

   //t1 and t2 have the same type here, that is Tuple<int, string, double>

   var t1 = new Tuple<int, string, double>(1, "foo", 2.0);
   var t2 = Tuple.New(1, "foo", 2.0);

Accessing elements can be done in a few ways:

   //Tuple<int, string, double, bool>

   var t1 = Tuple.New(2, "foo", 2.0, true);

   //access element using ElementN property
   int a = t1.Element1;

   //access element using ordinal number property
   string b = t1.Second;

   //access element using the indexer (requires cast)
   double c = (double)t1[2];

   //access element using Linq extension methods
   bool d = (bool)t1.ElementAt(3);

Easily append or prepend he tuple with another tuple or variable:

   //t1 will have the type of Tuple<string, int, string>
   var t1 = Tuple.New(1).Append("foo").Prepend("bar");

   //t3 will have the type of Tuple<int, string, int, string>
   var t2 = Tuple.New(2);
   var t3 = t2.Append(t1);

Easily convert a tuple to a list or create a tuple from a list:

   var t1 = Tuple.New(1, 2, "foo");

   //convert tuple to list using extension methods
   List<object> l1 = t1.ToList();

   //convert tuple to list, the classic way
   List<object> l2 = new List<object>(t1);

   //create a tuple from a list, using extension methods
   var t2 = l2.ToTuple<int, int, string>();

   //create a tuple from a list, without extension methods
   var t3 = Tuple.ToTuple<int, int, string>(l2);

   //t1, t2 and t3 are equal

And more ...

Last edited May 5, 2011 at 2:05 PM by HaAdis, version 11