概要
using System; using System.Collections; using System.Collections.Generic; class MyList : IEnumerable { private List<int> mList = new List<int>(); public void Add( int item ) { mList.Add( item ); } public IEnumerator GetEnumerator() { throw new NotImplementedException(); } } class Program { static void Main() { //var myList = new MyList(); //myList.Add( 1 ); //myList.Add( 2 ); //myList.Add( 3 ); var myList = new MyList{ 1, 2, 3 }; } }
- IEnumerable インターフェイスを実装する
- Add 関数を定義する
独自のクラスで List や配列をラップした場合でも
初期化処理をスマートに書けるようになります