代碼 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 { //構(gòu)造函數(shù) public Dog(params string[] dogNames) { DogNames.AddRange(dogNames); } //名稱字段、屬性 PRivate string name; public string Name { get { return name; } set { name = value; } } //List<string>集合元素?cái)?shù) public int DogNamesCounts() { return DogNames.Count; } public List<string> DogNames = new List<string>(); //Dog類對(duì)象所引器 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循環(huán)可以通過所引器訪問對(duì)象 //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循環(huán)遍歷int數(shù)組元素?
為什么代碼3不能通過foreach循環(huán)遍歷訪問
原因:
1.Dog類沒有實(shí)現(xiàn)IEnumerable接口(其實(shí)只需要有枚舉器方法即可:public IEnumerator GetEnumerator())
2.得有一個(gè)類實(shí)現(xiàn)IEnumerator接口
請(qǐng)參看下面代碼:
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;namespace ForeachDemo{ class Dog:IEnumerable { //構(gòu)造函數(shù) public Dog(params string[] dogNames) { DogNames.AddRange(dogNames); } //名稱字段、屬性 private string name; public string Name { get { return name; } set { name = value; } } //List<string>集合元素?cái)?shù) public int DogNamesCounts() { return DogNames.Count; } public List<string> DogNames = new List<string>(); //Dog類對(duì)象所引器 public string this[int index] { get { return DogNames[index]; } set { if (index >= DogNames.Count) { DogNames.Add(value); } else { DogNames[index] = value; } } } //這里需要一個(gè)IEnumerator類型的對(duì)象返回值(顯式實(shí)現(xiàn)IEnumerable) //IEnumerator IEnumerable.GetEnumerator() //{ // throw new NotImplementedException(); //} public IEnumerator GetEnumerator() { return new DogEnumerator(this.DogNames); } } public class DogEnumerator:IEnumerator { /* 顯式實(shí)現(xiàn)IEnumerator接口 object IEnumerator.Current { get { throw new NotImplementedException(); } } bool IEnumerator.MoveNext() { throw new NotImplementedException(); } void IEnumerator.Reset() { throw new NotImplementedException(); }*/ //構(gòu)造函數(shù) public DogEnumerator(List<string> paramDogNames) { this.dogsNames = paramDogNames; } List<string> dogsNames = new List<string>(); //枚舉器初始索引 private int index = -1; //獲取集合中的當(dāng)前元素 public object Current { get { if (index < 0) { return null; } else { return dogsNames[index]; } } } //判斷是否可以將枚舉數(shù)推進(jìn)到集合的下一個(gè)元素 public bool MoveNext() { index += 1; if (index >= dogsNames.Count) { return false; } else { return true; } } //將枚舉數(shù)設(shè)置為其初始位置,該位置位于集合中第一個(gè)元素之前(索引為-1) public void Reset() { this.index = -1; } } class Program { static void Main(string[] args) { Dog D = new Dog("哈士奇", "吉娃娃", "藏獒", "牧羊犬"); //for循環(huán)可以通過所引器訪問對(duì)象 //for (int i = 0; i < D.DogNames.Count; i++) //{ // Console.WriteLine(D[i].ToString()); //} foreach (string s in D) { Console.WriteLine(s); } Console.ReadKey(); } }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注