C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#で4タプルまたは4倍を作成するにはどうすればよいですか?


Tuple クラスは、4タプルを表します。これは4タプルと呼ばれます。タプルは、一連の要素を持つデータ構造です。

で使用されます
  • データセットへのより簡単なアクセス。
  • データセットの操作が簡単になります。
  • 単一のデータセットを表すため。
  • メソッドから複数の値を返すには
  • メソッドに複数の値を渡すには

ここで、C#で4タプルを実装する例を見てみましょう-

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<string,string,string,string> tuple = new Tuple<string,string,string,string>("nathan", "steve", "katie", "tim");
      Console.WriteLine("Value (Item1)= " + tuple.Item1);
      Console.WriteLine("Value (Item2)= " + tuple.Item2);
      Console.WriteLine("Value (Item3)= " + tuple.Item3);
      Console.WriteLine("Value (Item4)= " + tuple.Item4);
      if (tuple.Item1 == "nathan") {
         Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
      }
      if (tuple.Item2 == "jack") {
         Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
      }
      if (tuple.Item3 == "katie") {
         Console.WriteLine("Exists: Tuple Value = " +tuple.Item3);
      }
      if (tuple.Item4 == "tom") {
         Console.WriteLine("Exists: Tuple Value = " +tuple.Item4);
      }
   }
}

出力

これにより、次の出力が生成されます-

Value (Item1)= nathan
Value (Item2)= steve
Value (Item3)= katie
Value (Item4)= tom
Exists: Tuple Value = nathan
Exists: Tuple Value = katie

ここで、C#で4タプルを実装する別の例を見てみましょう-

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<int,int,int,int> tuple = new Tuple<int,int,int,int>(100, 150, 300, 450);
      Console.WriteLine("Value (Item1)= " + tuple.Item1);
      Console.WriteLine("Value (Item2)= " + tuple.Item2);
      Console.WriteLine("Value (Item3)= " + tuple.Item3);
      Console.WriteLine("Value (Item4)= " + tuple.Item4);
      if (tuple.Item1 == 100) {
         Console.WriteLine("Exists: Tuple Item 1 = " +tuple.Item1);
      }
      if (tuple.Item2 == 250) {
         Console.WriteLine("Exists: Tuple Item 2 = " +tuple.Item2);
      }
      if (tuple.Item3 == 270) {
         Console.WriteLine("Exists: Tuple Item 3 = " +tuple.Item3);
      }
      if (tuple.Item4 == 300) {
         Console.WriteLine("Exists: Tuple Item 4 = " +tuple.Item4);
      }
   }
}

出力

これにより、次の出力が生成されます-

Value (Item1)= 100
Value (Item2)= 150
Value (Item3)= 300
Value (Item4)= 450
Exists: Tuple Item 1 = 100

  1. 非リテラルのPythonタプルを作成するにはどうすればよいですか?

    最初にリストを作成し、次に変更する単一の値を変更し、最後に、リテラル以外のPythonタプルを作成する場合は、それをタプルに変換できます。たとえば、 def create_non_literal_tuple(a, b, c):    x = [1] * a    x[c] = b    return tuple(x) create_non_literal_tuple(6, 0, 2) これにより、出力が得られます: (1, 1, 0, 1, 1, 1) 長さ6の配列の位置2の0。

  2. Pythonで空のタプルを作成するにはどうすればよいですか?

    割り当てステートメントの括弧内に要素を指定しないことで、空のタプルオブジェクトを作成できます。空のタプルオブジェクトも、引数なしでtuple()組み込み関数によって作成されます >>> T1 = () >>> T1 () >>> T1 = tuple() >>> T1 ()