C# の Generics の使い方。
id:unibon:20080316:p7 の続き。
つぎのコードの Class1 クラスの Hoge1 メソッドのようにすればいいんだ。やっと分かった。
これで Class0 のようなクラスをわざわざ作らなくても良くなった。
using System; using System.Windows.Forms; using System.Collections; using System.Collections.Generic; namespace Hoge4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button7_Click(object sender, EventArgs e) { List<string> list0 = Class0<string>.Hoge0(); List<string> list1 = Class1.Hoge1<string>(); } } public class Class0<T0> { public static List<T0> Hoge0() { return new List<T0>(); } } public class Class1 { public static List<T1> Hoge1<T1>() { return new List<T1>(); } } }
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?mode=viewtopic&topic=45993&forum=7
を参考にした。