memo:C#ã®å復ã¨ä¾å¤
å復å¨ããã¡ã¢ãã¾ãã
1.å復ãã¾ã éGeneric
public class StampCollection : System.Collections.IEnumerable { private Dictionary<string, Stamp> stamps_ = new Dictionary<string, Stamp>(); public void Add(Stamp s) { stamps_.Add(s.Name, s); } public System.Collections.IEnumerator GetEnumerator() { //linq var ordertdStamp = from Stamp stamp in stamps_.Values orderby stamp.Year select stamp; foreach (Stamp stamp in ordertdStamp) yield return stamp; } public System.Collections.IEnumerable GetEnumerator2() { //linqã§ãªãã¼ã¹ var ordertdStamp = from Stamp stamp in stamps_.Values orderby stamp.Year descending select stamp; foreach (Stamp stamp in (IEnumerable<Stamp>)ordertdStamp) yield return stamp; } } public class Stamp { public int Year { get; set; } public string Name { get; set; } public Stamp(int year, string name) { this.Year = year; this.Name = name; } public override string ToString() { return this.Year + ":" + this.Name; } }
: System.Collections.IEnumerableãå®è£ ãããã¨ã§
StampCollection stamps = new StampCollection() { new Stamp(1998,"hoge1"), new Stamp(1999,"hoge2"), new Stamp(2000,"hoge3") }; foreach (Stamp s in stamps) Console.WriteLine(s.ToString()); foreach (Stamp s in stamps.GetEnumerator2()) Console.WriteLine(s.ToString());
{}ã§åæåæã«ããããçªã£è¾¼ããã(Addã®å®è£
ãå¿
è¦)
IEnumeratorãå®è£
è¦æ±ããã®ã¯System.Collections.IEnumerator GetEnumerator()ã§
ç¬èªã®å復ãå®ç¾©ããã¨ãã¯System.Collections.IEnumerable é¢æ°å()ã¨ãã風ã«
IEnumerableãè¿ãé¢æ°ãªã®ããã§ãã¯ãã¤ã³ãã
å©ç¨å´ã
foreach (Stamp s in stamps)
foreach (Stamp s in stamps.é¢æ°å())
ã¨ãªãã
2.ã²ããªãã¯
ãã¾ãã¡ç´å¾ã§ããªãã¦æ°æã¡æªããã©ãã¨ããããåãããåããããã
ã
public class ShopingList<T> : IEnumerable<T> { private List<T> items_ = new List<T>(); public ShopingList<T> Add(T name) { items_.Add(name); return this; } public IEnumerator<T> GetEnumerator() { return items_.GetEnumerator(); } //ç¬èªå復 public IEnumerable<T> GetEnumerator2() { foreach (T item in items_.Reverse<T>()) yield return item; } IEnumerator<T> IEnumerable<T>.GetEnumerator() { return GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } }
Genericçã¯å°ãæ¸ããã¨å¢ããæãï¼
å©ç¨ã¯åãããã«å¯è½
ShopingList<string> shopingcart = new ShopingList<string>() { "item1", "item2", "item3" }; //: IEnumerable<T>ãå®è£ ãã¦ãããããããªãã¨ãå¯è½âListã®å ´åã¯ãããªé¢¨ List<string> lis = new List<string>() { "hoge", "hoge" }; for (var i = 0; i < 10; i++) shopingcart.Add("name" + i.ToString()); foreach (string item in shopingcart) Console.WriteLine(item); foreach (string item in shopingcart.GetEnumerator2()) Console.WriteLine(item);
ãã ããåæã ããªã
class TestList<T> { List<T> lis_ = new List<T>(); public TestList<T> Add(T item) { lis_.Add(item); return this; } public IEnumerable<T> MyEnumerator() { foreach (T item in lis_) yield return item; } }
IEnumerableãè¿ãé¢æ°ã ãã§OK.
3.å復ä¸ã®ä¾å¤å¦ç
å復ä¸ã«ä¾å¤èµ·ãããã©ãããã®ãã¡ãã£ã¨æ°ã«ãªã£ã
ç¡è¶ã«ä¾å¤ãèµ·ãããã¨ããã£ããã©ããã¾ãããï¼
public IEnumerable<T> GetEnumeratorReigai() { foreach (T item in items_.Reverse<T>()) { if (item.Equals("item2")) { throw new Exception("ãããªãã¨ã¯èµ·ãããªãã®ã§ãããã...?ä¾å¤çºçããã¾ãã"); } else { //yield return ãtry-catchã¯ã§ããªã yield return item; } } }
ãã®ä¾å¤ã¯å復ãæ¢ãã¦ãã¾ãã
å©ç¨å´ã®ãâã®ç¨ã«æ¸ãã¦foreachãçµããã®ãã©ãã確ããã¦ã¿ãã
å復å®ç¾©ã®ä¸ã§èµ·ããä¾å¤ã¯å復ã®å¤å´ã®â»ï¼ã§catchãããã
yield returnãtry-catchã§ããªãã®ã§ã©ãã«ããªããªãããããããã®ä¾å¤ãèµ·ãããããªãã³å¼ãå¿
è³ã®ç·æ¥äºæ
ã«foreachãçµãããªãã§è¯ãå ´åã¨ã¯ã©ããªæãªã®ãï¼ã¨ãããã¨ãå°ãèãã¦ç´å¾ããã
foreachã¯çµãã£ã¦ãã¾ãã
try { foreach (string item in shopingcart.GetEnumerator2()) { try { Console.WriteLine(item); throw new Exception("foreachå é¨ã§ä¾å¤çºç"); } catch (Exception ex)//â»ï¼ { Console.WriteLine("in foreach:" + ex.ToString()); } } } catch (Exception ex) //â»ï¼ { Console.WriteLine("out of foreach:" + ex.ToString()); }
ä¸æ¹ã§å©ç¨å´ã§ä¾å¤ãçºçããå ´å(yield returnã ã¨ã©ã£ã¡ãå©ç¨å´ãªãã ãããããããªãããã)
throw new Exception("foreachå
é¨ã§ä¾å¤çºç");ã¯foreachãæ¢ããªãã
ä¾å¤çºçã®é½åº¦â»ï¼ã§catchãããã
ç¬èªã®ã¤ãã¬ã¼ã¿ããã£ã±ãä½ãã¨ãã¯LINQã®ç¾äººåº¦ã«ãã®ã®ãã
ãªãã ããã£ã¨ãã£ãããLINQã®ä½¿ãæ¹ã§ã§ããããããã©ãããã¯ã¾ãå¥ã®æ©ä¼ã«ãããã
ããã¾ã¼ã