C# タプル

WBOY
リリース: 2024-09-03 15:30:55
オリジナル
588 人が閲覧しました

C# タプルは、C#.net バージョン 4.0 で導入されたデータ構造です。タプル データ構造は、さまざまなデータ型の要素を保持するように設計されています。タプルは、クラス メソッドから 1 つのパラメーターで複数の値を返すのに役立ちます。これには、Out パラメーター、クラス型または構造体の型、または動的戻り型よりも多くの利点があります。パラメーターが単一のデータセットに渡されるため、このデータセットにアクセスしてさまざまな操作を実行することが簡単になります。

C# タプルを作成するには?

タプルは 2 つの異なる方法で作成できます

1.コンストラクター

の使用

タプルを作成するためのコンストラクターは Tuple に存在します。クラス。頭字語「T」は、タプルの作成時に指定された複数のデータ型を表します。タプルに格納される要素には 0 から 7 までの番号が付けられます。つまり、通常のタプルは 8 つの要素のみを保持し、8 つを超える要素を入力しようとすると、コンパイラはエラーをスローします。

単一要素タプル
Tuple <T1> (T1)
ログイン後にコピー

例:

Tuple<int> Tuple_example = new Tuple<int>(27);
Console.WriteLine(Tuple_example);
Console.ReadLine();
ログイン後にコピー

出力:

C# タプル

複数要素タプル
Tuple <T1, T2> (T1, T2)
ログイン後にコピー

例:

Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(1, "cat", true);
Console.WriteLine(tuple.Item1);
Console.WriteLine(tuple.Item2.ToString());
Console.ReadLine();
ログイン後にコピー

出力:

C# タプル

2.メソッドの作成

C# には、次のようにタプルを作成する静的な Create メソッドが用意されています

単一要素タプル
Create (T1);
ログイン後にコピー

例:

var Tuple_example = Tuple.Create(27);
Console.WriteLine(Tuple_example);
Console.ReadLine();
ログイン後にコピー

出力:

C# タプル

複数要素タプル
Create (T1, T2);
ログイン後にコピー

例:

var Tuple_example = Tuple.Create(1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
ログイン後にコピー
ログイン後にコピー

出力:

C# タプル

コンストラクターを使用する場合、タプルの作成時にすべての要素のデータ型を指定する必要があります。 Create メソッドは、上記のような面倒なコーディングを排除するのに役立ちます。

ValueTuple

ジェネリック タプルは参照型であり、値がヒープに格納されることを意味するため、メモリとパフォーマンスの点で使用するとコストが高くなります。 C#7.0 では、汎用タプルに代わって新しく改良されたバージョンのタプルが導入され、それを ValueTuple と名付けました。 ValueTuple はヒープに保存されるため、簡単に取得できます。値タプルは、.NET Framework 4.7 または .NET library 2.0 に付属しています。タプル機能を個別にインストールするには、System.Value.Tuple という NuGet パッケージをインストールする必要があります。

ValueTuple に関する重要なポイント

  • ValueTuple を作成するのは簡単です

例:

var Tuple_example = (1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
ログイン後にコピー

出力:

C# タプル

これは以下と同等です:

var Tuple_example = Tuple.Create(1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
ログイン後にコピー
ログイン後にコピー
  • ValueTuple は、「var」キーワードを使用せずに宣言することもできます。この場合、各メンバーのデータ型を指定する必要があります

例:

(int, string, bool) Tuple_example = (1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
ログイン後にコピー

出力:

C# タプル

  • 値は、
  • を使用して ValueTuple から返すことができます。

例:

details.Item1;   – returns 28
details.Item2; -- returns ”CBC”
ログイン後にコピー
  • ValueTuple は、通常のタプルとは異なり、要素を 1 つだけ含めることはできません。

例:

var detail = (28);  --this is not a tuple
var details = (28, “CBC”); -- this is a tuple
ログイン後にコピー

最初のステートメントでは、コンパイラーは 'detail' をタプルとして考慮せず、代わりに通常の 'var' 型とみなされます。

  • ValueTuple は、7 番目の位置に別のタプルをネストすることなく、8 つを超える値を保持できます。
  • ValueTuple のプロパティには、Item1、Item2 などとは異なる名前を付けることができます。
(int ID, String Firstname, string SecondName) details = (28, “CBC”, “C# Tuples”);
ログイン後にコピー
  • プログラミングの必要性に応じて、ValueTuples 内の要素を分離または破棄することもできます。上記の例では、要素「FirstName」を破棄し、最初の要素と 3 番目の要素を含むタプルをメソッドの戻り値の型として渡すことができます。

タプルはどのように機能しますか?

  1. C# フレームワークでは、タプル内に 8 つの要素のみが許可されます。つまり、0 から 7 までの値を持つことができ、それを超えるタプルを作成したい場合は、7 番目の要素 TRest をネストされたタプルとして指定します
var nestedtuple_example = new Tuple <int, string, string, int, int, int, string, Tuple<double, int, string>> (5, “This”, “is”, 7,8,9, “number”, Tuple.Create (17.33, 29,”April”));
ログイン後にコピー
  1. タプルの重要な使い方の 1 つは、従来の「out」キーワードと「ref」キーワードを使用せずに、タプルを単一のエンティティとしてメソッドに渡すことです。 「Out」パラメータと「ref」パラメータの使用は難しくて混乱する可能性があり、「out」パラメータと「ref」パラメータは「asnyc」メソッドでは機能しません。例えばpublic void TupleExampleMethod (Tuple tupleexample)
{
Var multiplication = tupleexample.Item1 * tupleexample.Item2;
Console.WriteLine (“Multiplication is”, {0}, multiplication);
}
ログイン後にコピー

メソッド TupleExampleMethod は次のようになります

TupleExampleMethod(new Tuple<int, int> (34,56));
ログイン後にコピー
  1. The dynamic keyword can also be used to return values from any method, but it is seldom used due to performance issues. The returning of the tuple from a method.
public static Tuple <int, string> GetPerson()
{
return Tuple.Create (1, “abc”);
}
ログイン後にコピー

Let’s create a program in Visual to understand how tuple works.

  • Launch Visual Studio and create a windows project.

C# タプル

  • We are creating a simple multiplication program that shows passing tuples by a method. A sample window created as below.

C# タプル

The values from both textboxes are taken into a tuple and the tuple is passed on to a method.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnMultiply_Click(object sender, EventArgs e)
{
int value1 = Convert.ToInt32(txtVal1.Text);
int value2 = Convert.ToInt32(TxtVal2.Text);
CallMethod(new Tuple<int, int>(value1, value2));
}
private void CallMethod(Tuple<int, int> tuple)
{
txtResult.Text = Convert.ToString(tuple.Item1 * tuple.Item2);
Console.ReadLine();
}
}
}
ログイン後にコピー

The result is displayed in the third text box named as txtResult. End result looks like.

C# タプル

Conclusion

The tuple data structure is a reference type, which means the values are stored on the heap instead of stack. This makes usage of tuples and accessing them in the program an intensive CPU task. The only 8 elements in tuples property is one of the major drawbacks of tuples as nested tuples are more prone to induce ambiguity. Also accessing elements in tuple with Item is also ambiguous as one must remember what position the element is on in order to access it. C#7 has introduced ValueTuple which is value type representation of tuple. It works only on .NET Framework 4.7 and hence needs to install separately from the Nuget package System.ValueTuple package.

以上がC# タプルの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!