国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#之foreach實(shí)現(xiàn)探秘

2019-11-17 02:14:50
字體:
供稿:網(wǎng)友

C#之foreach實(shí)現(xiàn)探秘

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 卫辉市| 五莲县| 盘山县| 永城市| 手机| 青岛市| 阜宁县| 龙井市| 天柱县| 长海县| 金湖县| 东台市| 新和县| 青铜峡市| 康保县| 嘉义市| 安顺市| 屏东市| 长葛市| 玉林市| 肇东市| 德州市| 临清市| 漳平市| 临高县| 岚皋县| 葵青区| 普兰店市| 阿鲁科尔沁旗| 综艺| 永德县| 黄平县| 栾城县| 牟定县| 剑阁县| 牡丹江市| 永济市| 佛坪县| 桦甸市| 萝北县| 武隆县|