型情報を引数に指定可能なクラスを作成する
型情報を引数に指定可能なクラスを作成する
型情報を引数に指定することの出来るクラスをジェネリッククラスと呼びます。
ジェネリッククラスは以下のように定義します。
[アクセス修飾子] class クラス名‹識別子›
{
…
}
識別子には分かりやすい名前をつけるか、一般に使われる"T"と言う文字を指定します。
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Sample‹int› sampleA = new Sample‹int›(); sampleA.A = "型情報を引数に指定可能なクラスのテスト"; MessageBox.Show("sampleA a:"+sample.A); Sample‹string› sampleB = new Sample‹string›(); sampleB.A = "型情報を引数に指定可能なクラスのテスト"; MessageBox.Show("sampleB a:"+sample.A); } } public class SampleB‹T› { private T a; public T A { set { a = value; } get { return a; } } } }