首页 > 学院 > 开发设计 > 正文

C#之foreach实现探秘

2019-11-17 02:14:50
字体:
来源:转载
供稿:网友

C#之foreach实现探秘

  代码 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();        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表