C # Use of Tuple (T1, t2... T) tuples

Source: Internet
Author: User

C # Use of Tuple (T1, t2... T) tuples

1)

Group element: a data structure separated by commas (,). It is a combination of a series of values passed to a program or operating system.

NET Framework directly supports one to seven elements of tuples

Tuple Tuple Tuple Tuple Tuple Tuple Tuple
In addition, you can create eight or more elements of the Tuple In the Rest attribute through the objects in the nested tuples. Object.

Simple Example:
// The Tuple of an element
         
          
Test = new Tuple
          
           
(34); // The tuples of the two elements 1
           
            
Test2 = Tuple. Create
            
             
("Str", 2); Tuple
             
              
Test2_1 = new Tuple
              
                (); // The tuples of 8 elements (Note: Tuple <type...>: a maximum of 7 basic "types", and the eighth element type must also be tuples) Tuple
               
                 > Test3 = new Tuple
                
                  > (1, 2, 3, 4, 5, 6, 7, new Tuple
                 
                   (8); // Tuple
                  
                    > Test_ I _ I _Tii = new Tuple
                   
                     > (1, 1, new Tuple
                    
                      (2, 3); Console. writeLine (test. item1); Console. writeLine (test2.Item1 + test2.Item2); Console. writeLine (test2_1.Item1 + test2_1.Item2); Console. writeLine (test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest. item1 );
                    
                   
                  
                 
                
               
              
             
            
           
          
         
Result:


2) Multiple return values
Generally, we use the out keyword (which is a little more troublesome than other languages such as golang and out keyword). At this time, we can use the tuples for implementation:
Namespace TupleDemo {class Program {static void Main (string [] args) {// use out to get multiple returned values string outparam = ""; int returnvalue = FunOutParamDemo (out outparam ); console. writeLine (returnvalue + "" + outparam); // use the tuples to get multiple returned values. Tuple
         
          
R = FunTupleParamDemo (); Console. WriteLine (r. Item1 + "" + r. Item2); Console. Read ();}///
          /// Out keyword, which returns two return values //////
          ///
          Public static int FunOutParamDemo (out string o) {o = "returnValue"; return 10 ;}///
          /// Use tuples to achieve [indirect] return [two] return values //////
          Public static Tuple
          
           
FunTupleParamDemo () {return new Tuple
           
            
(10, "returnValue ");}}}
           
          
         
Running result:



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.