第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

C# 什么時(shí)候需要實(shí)現(xiàn)IEnumerator,IEnumerable這2個(gè)接口?

C# 什么時(shí)候需要實(shí)現(xiàn)IEnumerator,IEnumerable這2個(gè)接口?

慕碼人8056858 2018-12-07 02:20:25
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Collections; using System.Data.Common; using System.Data; using System.Security.Util; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { Person[] ps = { new Person("a", 1), new Person("b", 2), new Person("c", 3) }; People p = new People(ps); foreach (Person item in p) { Console.WriteLine("name={0},age={1}",item.Name,item.Age); } Console.Read(); } } class Person { public string Name; public int Age; public Person(string name, int age) { Name = name; Age = age; } } class PersonEnum : IEnumerator { Person []ps; public PersonEnum(Person[]ps) { this.ps=ps; } int position = -1; public void Reset (){ position=-1;} public bool MoveNext() { ++position; return position < ps.Length; } public object Current { get { return ps[position];} } } class People:IEnumerable { Person[] ps; public People(Person[]ps) { this.ps = ps; } public IEnumerator GetEnumerator() { return new PersonEnum(ps); } } } 上邊例子中,如果將Main中的代碼換成下邊這樣子也能實(shí)現(xiàn)foreach啊,為什么要像上面這樣寫呢? Person[] ps = { new Person("a", 1), new Person("b", 2), new Person("c", 3) }; foreach (Person item in ps) { Console.WriteLine("name={0},age={1}",item.Name,item.Age); } Console.Read();
查看完整描述

4 回答

?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊

用到數(shù)組或者集合的時(shí)候都要實(shí)現(xiàn)

查看完整回答
反對(duì) 回復(fù) 2019-01-21
?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

本質(zhì)就是對(duì)設(shè)計(jì)模式中迭代器模式的一個(gè)實(shí)現(xiàn),建議看下這個(gè)模式就知道背后的原理了

查看完整回答
反對(duì) 回復(fù) 2019-01-21
  • 4 回答
  • 0 關(guān)注
  • 403 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)