代碼 1:
static void Main(string[] args) { int[] hs = { 1,2,3,4,5,6,7,8,9}; foreach (int item in hs) { Console.WriteLine(item.ToString()); } Console.ReadKey(); }
代碼 2:
static void Main(string[] args) { ArrayList ArrLst = new ArrayList(); ArrLst.AddRange(new string[] { "jack","rose","joy","kristina"}); foreach (string s in ArrLst) { Console.WriteLine(s); } Console.ReadKey(); }
代碼 3:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ForeachDemo { class Dog { //構造函數 public Dog(params string[] dogNames) { DogNames.AddRange(dogNames); } //名稱字段、屬性 private string name; public string Name { get { return name; } set { name = value; } } //List<string>集合元素數 public int DogNamesCounts() { return DogNames.Count; } public List<string> DogNames = new List<string>(); //Dog類對象所引器 public string this[int index] { get { return DogNames[index];} set { if (index >= DogNames.Count) { DogNames.Add(value); } else { DogNames[index] = value; } } } } class Program { static void Main(string[] args) { Dog D = new Dog("哈士奇","吉娃娃","藏獒","牧羊犬"); //for循環可以通過所引器訪問對象 //for (int i = 0; i < D.DogNames.Count; i++) //{ // Console.WriteLine(D[i].ToString()); //} //foreach卻不能通過所引器遍歷 foreach (string s in D) { Console.WriteLine(s); } Console.ReadKey(); } } }
問題:為什麼代碼1 和代碼2可以通過foreach循環遍歷int數組元素?
為什麼代碼3不能通過foreach循環遍歷訪問
原因:
1.Dog類沒有實現IEnumerable接口(其實只需要有枚舉器方法即可:public IEnumerator GetEnumerator())
2.得有一個類實現IEnumerator接口
請參看下面代碼:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ForeachDemo { class Dog:IEnumerable { //構造函數 public Dog(params string[] dogNames) { DogNames.AddRange(dogNames); } //名稱字段、屬性 private string name; public string Name { get { return name; } set { name = value; } } //List<string>集合元素數 public int DogNamesCounts() { return DogNames.Count; } public List<string> DogNames = new List<string>(); //Dog類對象所引器 public string this[int index] { get { return DogNames[index]; } set { if (index >= DogNames.Count) { DogNames.Add(value); } else { DogNames[index] = value; } } } //這裡需要一個IEnumerator類型的對象返回值(顯式實現IEnumerable) //IEnumerator IEnumerable.GetEnumerator() //{ // throw new NotImplementedException(); //} public IEnumerator GetEnumerator() { return new DogEnumerator(this.DogNames); } } public class DogEnumerator:IEnumerator { /* 顯式實現IEnumerator接口 object IEnumerator.Current { get { throw new NotImplementedException(); } } bool IEnumerator.MoveNext() { throw new NotImplementedException(); } void IEnumerator.Reset() { throw new NotImplementedException(); }*/ //構造函數 public DogEnumerator(List<string> paramDogNames) { this.dogsNames = paramDogNames; } List<string> dogsNames = new List<string>(); //枚舉器初始索引 private int index = -1; //獲取集合中的當前元素 public object Current { get { if (index < 0) { return null; } else { return dogsNames[index]; } } } //判斷是否可以將枚舉數推進到集合的下一個元素 public bool MoveNext() { index += 1; if (index >= dogsNames.Count) { return false; } else { return true; } } //將枚舉數設置為其初始位置,該位置位於集合中第一個元素之前(索引為-1) public void Reset() { this.index = -1; } } class Program { static void Main(string[] args) { Dog D = new Dog("哈士奇", "吉娃娃", "藏獒", "牧羊犬"); //for循環可以通過所引器訪問對象 //for (int i = 0; i < D.DogNames.Count; i++) //{ // Console.WriteLine(D[i].ToString()); //} foreach (string s in D) { Console.WriteLine(s); } Console.ReadKey(); } } }