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

首頁 > 學院 > 開發設計 > 正文

實現Foreach遍歷

2019-11-17 03:14:17
字體:
來源:轉載
供稿:網友

實現Foreach遍歷

實現Foreach遍歷的集合類,需要實現IEnumerable接口,泛型集合則需要實現IEnumerable<T>接口

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;namespace Foreach{    public  class ListForeach<T> :IEnumerable<T>  where T :class ,new()    {        PRivate  T[] array;        private int index = 0;        public ListForeach(int  capcity)        {            array = new T[capcity];        }        public int Count        {            get            {                return index;            }        }        public void Add(T value)         {                       if (index >= array.Length)              {                 T[] newArr = new T[array.Length * 2];                //將原來的數組中的數據拷貝到新數組中.                Array.Copy(array, newArr, array.Length);                //然后將舊數組的變量指向新數組                array = newArr;            }            array[index++] = value;        }        public IEnumerator<T> GetEnumerator()        {            return new TEnumeratro<T>(array, index);                    }        IEnumerator IEnumerable.GetEnumerator()        {            return GetEnumerator();        }    }    public class  TEnumeratro<T> : IEnumerator<T>    {         private  T [] arr ;         private int count;         private int position = -1;         public TEnumeratro(T [] array, int count)        {            this.arr = array;            this.count = count;        }        public T Current        {            get {                return arr[position];            }        }        public void Dispose()        {            arr = null;        }        object IEnumerator.Current        {            get {                return  this.Current;            }        }        public bool MoveNext()        {            position++;            if (position < this.count)                return true;            else                return false;        }        public void Reset()        {            this.position = -1;        }    }}

客戶端代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Foreach{    public class Student    {        public int Age {get;set; }        public string  Name { get; set; }            }    class Program    {        static void Main(string[] args)        {            ListForeach<Student> list = new ListForeach<Student>(10);            list.Add(new Student() {Age=111,Name="123" });            list.Add(new Student() { Age = 222, Name = "222" });            list.Add(new Student() { Age = 333, Name = "111" });            list.Add(new Student() { Age = 444, Name = "23222" });            list.Add(new Student() { Age = 22552, Name = "32" });            list.Add(new Student() { Age = 222, Name = "1111334" });            foreach (var s in list)            {                Console.WriteLine(s.Name+":"+ s.Age);            }            ListForeach<Student> list1 = new ListForeach<Student>(10) {             new Student(){ Age=666,Name="yjq"},            new Student(){ Age=88,Name="dddd"},            new Student(){ Age=999,Name="ddd"},            new Student(){ Age=000,Name="==="},            };            foreach (var s in list1)            {                Console.WriteLine(s.Name + ":" + s.Age);            }            Console.Write(list.Count);            Console.Read();                   }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长子县| 吴忠市| 咸阳市| 靖边县| 渝中区| 蓬安县| 锦屏县| 离岛区| 南丹县| 西青区| 乐安县| 英吉沙县| 菏泽市| 和田县| 久治县| 玉树县| 鄂州市| 东阿县| 星座| 房产| 稻城县| 邵武市| 抚宁县| 响水县| 金阳县| 萨嘎县| 东宁县| 梓潼县| 中宁县| 武安市| 固阳县| 淄博市| 长岭县| 铁岭市| 承德市| 常宁市| 屏边| 临海市| 华宁县| 鲁甸县| 鲁甸县|