Tuple Type Added to C# 4.0

With C# 4.0 you can now use the new Tuple type instead of creating your very own… far simpler than using a struct or class to accomplish the same task.

There are two ways to create a Tuple in c# 4.0:

var newTuple = new Tuple<string, string, int>("Sean", "Jaeger", 36);
var newTuple = Tuple.Create<string, string, int>("Sean", "Jaeger", 36);


Type inference can also be used (but doesn’t self document):



var newTuple = new Tuple.Create("Sean", "Jaeger", 36);


You can add up to eight parameters now:



var newTuple = new Tuple<int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8);


One huge thing I see in using a Tuple rather than an anonymous type is the fact that scope can be at any level.  As we all know the scope of an anonymous type is limited to the method boundaries.



Fun stuff!

0 comments:

Post a Comment