Tuesday, May 17, 2016

C# Tuple

-       If you need quickly to  :
o   Pass multiple values to a method through a single parameter.
o   to return multiple values from a method without using out parameters

-       Tuple type is a class. Once we create the Tuple, we cannot change the values of its fields.

Example1:

Tuple<int, string> tuple = new Tuple<int, string>(33, "NADY");

Example2:

Tuple has 8 overloads and each of these overloads returns new object of Tuple<t1, t2...t8> class.  

However, if you write:
Tuple<int, string, int, int, int, int, int, int> tuple =
new Tuple<int, string, int, int, int, int, int, int>(33, "NADY", 39, 39, 59, 49, 30, 33);

-       You will get ArgumentException



-       Therefore, you should pass the last generic argument 8th as Tuple. Using this argument, you can create as many arguments as you want. 
-       Tuple<int, string, int, int, int, int, int, Tuple<int>> tuple =
new Tuple<int, string, int, int, int, int, int, Tuple<int>>(20, "NADY", 39, 39, 59, 49, 30, new Tuple<int>(33));

 












Result As CSV:

If you use ToString with Tuple objects, it will return comma separated string with braces around it. 





Using Out PARAM Example:



Using Tuple:




 naadydev@outlook.com
www.mohnady.com

4 comments:

Anonymous said...

شكرا لك اخي نادي
ولكن حبذا لو كانت المقالة بالعربي لدعم المحتوى العربي

Aladdin Bahian said...

يعطيك الف عافية شرح ممتاز

Aladdin Bahian said...

يعطيك الف عافية شرح ممتاز

Anonymous said...

Nice article, thanks