Search code examples
c#.netgenerics

number of elements in Tuple<...>


Just wondering if there's an easy way to know how many elements are contained in a Tuple class

eg.

 var a = new Tuple<int,int>(1,2);

but how many elements are there? Perhaps we don't care if we try to cast via the as keyword

 var a1 = a as Tuple<int>
 if(a1!=null)

 var a2 = a as Tuple<int,int>
 if(a2!=null)

Just after a little feedback. Are many people using Tuple?


Solution

  • var a = new Tuple<int, int>(1, 2);
    var aType = a.GetType();
    var numberOfGenericParameters = aType.GetGenericArguments().Length;