c# - How to use Tuple.Create() method to create an tuple like this -


if write code this:

var = tuple.create(1,2,3); var b = tuple.create(1,2,3,4,5,6,7,a); 

it creates tuple type is:

tuple<int,int,int,int,int,int,int,tuple<tuple<int,int,int>>> 

how use tuple.create() method create tuple like:

tuple<int,int,int,int,int,int,int,tuple<int,int,int>> 

as documentation says, return value of tuple.create<t1, t2, t3, t4, t5, t6, t7, t8> method system.tuple<t1, t2, t3, t4, t5, t6, t7, tuple<t8>> 8-th argument wrapped new tuple. if passing tuple last argument, tuple wrapped tuple , you'll tuple<tuple<>> have now.

implementation of creation method pretty straight-forward:

static tuple<t1, t2, t3, t4, t5, t6, t7, tuple<t8>> create<t1, t2, t3, t4, t5, t6, t7, t8>(    t1 item1, t2 item2, t3 item3, t4 item4, t5 item5, t6 item6, t7 item7, t8 item8) {     return new tuple<t1, t2, t3, t4, t5, t6, t7, tuple<t8>>(         item1, item2, item3, item4, item5, item6, item7, new tuple<t8>(item8)); } 

as can see last argument wrapped , passed constructor. there nothing can here.

but have @ least 2 options. can use tuple constructor directly. of course, have specify tuple type manually in case. or can create own creation method without last argument wrapping, type inference you:

static tuple<t1, t2, t3, t4, t5, t6, t7, t8> createtuple<t1, t2, t3, t4, t5, t6, t7, t8>(     t1 item1, t2 item2, t3 item3, t4 item4, t5 item5, t6 item6, t7 item7, t8 item8)     t8: ituple {     return new tuple<t1, t2, t3, t4, t5, t6, t7, t8>(         item1, item2, item3, item4, item5, item6, item7, item8); } 

usage

var = tuple.create(1,2,3); var b = createtuple(1,2,3,4,5,6,7,a); // tuple<int,int,int,int,int,int,int,tuple<int,int,int>> 

note c# 7 have value tuples provide better interface nicely named properties. e.g.

var point = (x: 1, y: 2, z: 3); var rocket = (id: 1, speed: 100, location: point);  // rocket.location.y 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -