Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number of elements in Tuple<...>

Tags:

c#

.net

generics

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?

like image 523
sgtz Avatar asked Oct 03 '11 05:10

sgtz


1 Answers

var a = new Tuple<int, int>(1, 2);
var aType = a.GetType();
var numberOfGenericParameters = aType.GetGenericArguments().Length;
like image 79
Albin Sunnanbo Avatar answered Oct 10 '22 11:10

Albin Sunnanbo