C#中要使一个类支持FOREACH遍历,实现过程怎样?

admin2019-03-29  58

问题 C#中要使一个类支持FOREACH遍历,实现过程怎样?

选项

答案public class DaysOfTheWeek : System.Collections.Ienumerable { string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; public System.Collections.IEnumerator GetEnumerator() { for (int i = 0; i < m_Days.Length; i++) { yield return m_Days[i]; } } } class TestDaysOfTheWeek{ static void Main() { // Create an instance of the collection class DaysOfTheWeek week = new DaysOfTheWeek(); // Iterate with foreach foreach (string day in week) { System.Console.Write(day + " "); } }}

解析
转载请注明原文地址:https://jikaoti.com/ti/qfg7FFFM
0

相关试题推荐
最新回复(0)