c# Tuple

Tuple


使用场景

  • To represent multiple data into a single data set.

  • To create, manipulate, and access data set.

  • To return multiple values from a method without using out parameters.

  • To pass multiple values to a method with the help of single parameters.

构造方法

  1. Constructor

// Constructor for single elements
Tuple (T1)
// Constructor for two elements
Tuple <T1, T2>(T1, T2)
// Constructor for eight elements
Tuple <T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)

using System;
   
public class GFG{
       
    // Main method
    static public void Main (){
           
        // Tuple with one element
    Tuple<string>My_Tuple1 = new Tuple<string>("GeeksforGeeks");
       
    // Tuple with three elements
    Tuple<string, string, int>My_Tuple2 = new Tuple<string, string, int>("Romil", "Python", 29);
       
    // Tuple with eight elements
    Tuple<int, int, int, int, int, int, int, Tuple<int>>My_Tuple3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1,2,3,4,5,6,7, new Tuple<int>(8));
    }
}
  1. Create

// Method for 1-tuple
Create(T1)
// Method for 2-tuple
Create(T1, T2)
// Method for 8-tuple
Create(T1, T2, T3, T4, T5, T6, T7, T8)

using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 1-tuple
        // Using Create Method
        var My_Tuple1 = Tuple.Create("GeeksforGeeks");
  
        // Creating 4-tuple
        // Using Create Method
        var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
  
        // Creating 8-tuple
        // Using Create Method
        var My_Tuple3 = Tuple.Create(13, "Geeks", 67, 
                      89.90, 'g', 39939, "geek", 10);
    }
}

Accessing a Tuple

using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 1-tuple
        // Using Create Method
        var My_Tuple1 = Tuple.Create("GeeksforGeeks");
  
        // Accessing the element of Tuple
        // Using Item property
        Console.WriteLine("Element of My_Tuple1: " + My_Tuple1.Item1);
        Console.WriteLine();
  
        // Creating 4-tuple
        // Using Create Method
        var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
  
        // Accessing the element of Tuple
        // Using Item property
        Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item1);
        Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item2);
        Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item3);
        Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item4);
        Console.WriteLine();
  
        // Creating 8-tuple
        // Using Create Method
        var My_Tuple3 = Tuple.Create(13, "Geeks",
              67, 89.90, 'g', 39939, "geek", 10);
  
        // Accessing the element of Tuple
        // Using Item property
        // And print the 8th element of tuple
        // using Rest property
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item1);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item2);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item3);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item4);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item5);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item6);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item7);
        Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Rest);
    }
}

Nested Tuples


// C# program to illustrate nested tuple
using System;
  
public class GFG{
      
        // Main method
    static public void Main ()
        {
          
           // Nested Tuple
        var My_Tuple = Tuple.Create(13, "Geeks", 67, 89.90,
                 'g', 39939, "geek", Tuple.Create(12, 30, 40, 50));
          
        // Accessing the element of Tuple
        // Using Item property
        // And accessing the elements of nested tuple
        // Using Rest property
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item1);
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item2);
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item3);
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item4);
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item5);
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item6);
        Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item7);
        Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest);
        Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item1);
        Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item2);
        Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item3);
        Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item4);
    }
}

Limitation of Tuple

  • It is of reference type not of value type.
  • It is limited to eight elements. Means you cannot store more than eight elements without nested tuple.
  • These are only accessed by using Item property.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值