タプルを利用して一つの変数内に複数の値を格納する - C#

タプルを利用して一つの変数内に複数の値を格納するコードを紹介します。

概要

C#7 (.NET Framework 4.7)では、「タプル」と呼ばれる新しい機能を利用できます。タプルを用いると一つの変数内に複数の値を格納できます。

書式

([型名] [変数名], [型名] [変数名], ....) 変数名

書式例

(int x, int y, int z) position;

プログラム

UI

下図のUIを作成します。フォームにButtonと複数行のTextBoxを配置します。

コード

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 SimpleTuple
{
  public partial class FormTuple : Form
  {
    public FormTuple()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      (int x, int y) position;

      position.x = 24;
      position.y = 18;
      textBox1.Text += string.Format("(x, y)=({0:d},{1:d})", position.x, position.y);
    }
  }
}

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


[button1]をクリックします。タプルの変数に値が代入されます。タプルに代入された値を取得しテキストボックスに表示されます。

代入に関する書式 (リテラルの記述)

タプルに代入する書式として以下が利用できます。
書式1
private void button1_Click(object sender, EventArgs e)
{
  (int x, int y) position;

  position.x = 24;
  position.y = 18;
  textBox1.Text += string.Format("(x, y)=({0:d},{1:d})", position.x, position.y);
}

書式2
private void button1_Click(object sender, EventArgs e)
{
  (int x, int y) position;
     
  position = (8, 12);
  textBox1.Text += string.Format("(x, y)=({0:d},{1:d})", position.x, position.y);
}

書式3
private void button1_Click(object sender, EventArgs e)
{
  (int x, int y) position;

  position = (x:64, y:56);
  textBox1.Text += string.Format("(x, y)=({0:d},{1:d})", position.x, position.y);
}

プログラム2:複数の型を持つ例

複数の型をタプルに含めることもできます。

UI

下図のUIを作成します。フォームにButtonと複数行のTextBoxを配置します。今回は"button2"のみを利用します。

コード

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 SimpleTuple
{
  public partial class FormTuple : Form
  {
    public FormTuple()
    {
      InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      (int x, int y, string name, string tag, double point) data;

      data = (x: 38, y: 14, name:"ぺんぎん", tag: "PEN", point:3.45);

      textBox1.Text += string.Format("(x, y, point)=({0:d},{1:d},{2:f})\r\n", data.x, data.y, data.point);
      textBox1.Text += string.Format("(name, tag)=({0},{1})\r\n", data.name, data.tag);
    }
  }
}

解説

(int x, int y, string name, string tag, double point) data;
のコードにより、int型の変数2つ、string型の変数2つ、double型の変数1つを持つタプルを宣言しています。

data = (x: 38, y: 14, name:"ぺんぎん", tag: "PEN", point:3.45);
のコードにより、タプルに値を代入しています。

textBox1.Text += string.Format("(x, y, point)=({0:d},{1:d},{2:f})\r\n", data.x, data.y, data.point);
textBox1.Text += string.Format("(name, tag)=({0},{1})\r\n", data.name, data.tag);
タプルから値を取り出し、テキストボックスに表示します。

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


[button2]をクリックします。タプルの変数に代入された内容がテキストボックスに表示されます。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2022-11-04
作成日: 2017-04-18
iPentec all rights reserverd.