自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

2021年了,`IEnumerator`、`IEnumerable`接口還傻傻分不清楚?

網(wǎng)絡(luò) 通信技術(shù)
IEnumerator、IEnumerable這兩個(gè)接口單詞相近、含義相關(guān),傻傻分不清楚。入行多年,一直沒(méi)有系統(tǒng)性梳理這對(duì)李逵李鬼。

[[375509]]

本文轉(zhuǎn)載自微信公眾號(hào)「全棧碼農(nóng)畫(huà)像」,作者小碼甲  。轉(zhuǎn)載本文請(qǐng)聯(lián)系全棧碼農(nóng)畫(huà)像公眾號(hào)。

 IEnumerator、IEnumerable這兩個(gè)接口單詞相近、含義相關(guān),傻傻分不清楚。

入行多年,一直沒(méi)有系統(tǒng)性梳理這對(duì)李逵李鬼。

最近本人在懟著why神的《其實(shí)吧,LRU也就那么回事》,方案1使用數(shù)組實(shí)現(xiàn)LRU,手寫(xiě)算法涉及這一對(duì)接口,借此時(shí)機(jī)覆蓋這一對(duì)難纏的冤家。

IEnumerator

IEnumerator、IEnumerable接口有相似的名稱(chēng),這兩個(gè)接口通常也在一起使用,它們有不同的用途。

IEnumerator接口為類(lèi)內(nèi)部的集合提供了迭代方式, IEnumerator 要求你實(shí)現(xiàn)三個(gè)方法:

  1. MoveNext方法:該方法將集合索引加1,并返回一個(gè)bool值,指示是否已到達(dá)集合的末尾。
  2. Reset方法:它將集合索引重置為其初始值-1,這會(huì)使枚舉數(shù)無(wú)效。
  3. Current方法: 返回position位置的當(dāng)前對(duì)象

IEnumerable

IEnumerable接口為foreach迭代提供了支持,IEnumerable要求你實(shí)現(xiàn)GetEnumerator方法。

  1. public IEnumerator GetEnumerator() 
  2.     return (IEnumerator)this; 

該用哪一個(gè)接口?

僅憑以上辭藻,很難區(qū)分兩個(gè)接口的使用場(chǎng)景。

IEnumerator接口定義對(duì)類(lèi)中的集合類(lèi)型對(duì)象的迭代方式,

IEnumerable接口允許使用foreach循環(huán)進(jìn)行枚舉。

因此IEnumerable接口的GetEnumerator方法會(huì)返回一個(gè)IEnumerator接口。要實(shí)現(xiàn)IEnumerable,你還必須實(shí)現(xiàn)IEnumerator。

從英文詞根上講:

IEnumerator接口代表了枚舉器,里面定義了枚舉方式,是名詞。

IEnumerable接口代表該對(duì)象具備了可被枚舉的性質(zhì),是形容詞。

總之,如果您想提供對(duì)foreach的支持,那么就先讓對(duì)象可枚舉,再談?wù)撁杜e方式,也就是說(shuō)實(shí)現(xiàn)這兩個(gè)接口。

最佳實(shí)踐

  • 在嵌套類(lèi)中實(shí)現(xiàn)IEnumerator,這樣你可以創(chuàng)建多個(gè)枚舉器。
  • 為IEnumerator的Current方法提供異常處理。

為什么要這么做?

如果集合的內(nèi)容發(fā)生變化,則reset方法將被調(diào)用,緊接著當(dāng)前枚舉數(shù)無(wú)效,您將收到一個(gè)IndexOutOfRangeException異常(其他情況也可能導(dǎo)致此異常)。所以執(zhí)行一個(gè)Try…Catch塊來(lái)捕獲這個(gè)異常并引發(fā)InvalidOperationException異常, 提示在迭代時(shí)不允許修改集合內(nèi)容。

“這也正是我們常見(jiàn)的在foreach 里面嘗試修改迭代對(duì)象會(huì)報(bào)InvalidOperationException異常的原因。

下面以汽車(chē)列表為例實(shí)現(xiàn)IEnumerator IEnumerable接口

  1. using System; 
  2. using System.Collections; 
  3. namespace ConsoleEnum 
  4.     public class cars : IEnumerable 
  5.     { 
  6.         private car[] carlist; 
  7.    
  8.         //Create internal array in constructor. 
  9.         public cars() 
  10.         { 
  11.             carlist= new car[6] 
  12.             { 
  13.                 new car("Ford",1992), 
  14.                 new car("Fiat",1988), 
  15.                 new car("Buick",1932), 
  16.                 new car("Ford",1932), 
  17.                 new car("Dodge",1999), 
  18.                 new car("Honda",1977) 
  19.             }; 
  20.         } 
  21.         //private enumerator class 
  22.         private class  MyEnumerator:IEnumerator 
  23.         { 
  24.             public car[] carlist; 
  25.             int position = -1; 
  26.  
  27.             //constructor 
  28.             public MyEnumerator(car[] list) 
  29.             { 
  30.                 carlist=list; 
  31.             } 
  32.             private IEnumerator getEnumerator() 
  33.             { 
  34.                 return (IEnumerator)this; 
  35.             } 
  36.             //IEnumerator 
  37.             public bool MoveNext() 
  38.             { 
  39.                 position++; 
  40.                 return (position < carlist.Length); 
  41.             } 
  42.             //IEnumerator 
  43.             public void Reset() 
  44.             { 
  45.                 position = -1; 
  46.             } 
  47.             //IEnumerator 
  48.             public object Current 
  49.             { 
  50.                 get 
  51.                 { 
  52.                     try 
  53.                     { 
  54.                         return carlist[position]; 
  55.                     } 
  56.                     catch (IndexOutOfRangeException) 
  57.                     { 
  58.                         throw new InvalidOperationException(); 
  59.                     } 
  60.                 } 
  61.             } 
  62.         }  //end nested class 
  63.       public IEnumerator GetEnumerator() 
  64.       { 
  65.           return new MyEnumerator(carlist); 
  66.       } 
  67.     } 

在foreach cars的時(shí)候,可以明顯看到

  • foreach語(yǔ)法糖初次接觸可枚舉的cars, 實(shí)際會(huì)訪(fǎng)問(wèn)cars實(shí)現(xiàn)的 GetEnumerator()方法,拿到迭代器
  • foreach每次迭代,實(shí)際會(huì)訪(fǎng)問(wèn)迭代器的Current屬性

 

 

責(zé)任編輯:武曉燕 來(lái)源: 全棧碼農(nóng)畫(huà)像
相關(guān)推薦

2021-03-10 08:56:37

Zookeeper

2021-07-27 07:31:16

JavaArrayList數(shù)組

2022-05-15 21:52:04

typeTypeScriptinterface

2024-02-29 09:08:56

Encoding算法加密

2020-10-30 08:20:04

SD卡TF卡存儲(chǔ)

2018-12-17 12:30:05

Kubernetes存儲(chǔ)存儲(chǔ)卷

2018-05-22 16:24:20

HashMapJavaJDK

2020-03-03 17:35:09

Full GCMinor

2023-02-27 15:46:19

數(shù)據(jù)元元數(shù)據(jù)

2023-09-03 21:18:07

Python編程語(yǔ)言

2022-02-25 09:14:33

類(lèi)變量共享實(shí)例變量

2016-11-04 12:51:46

Unix網(wǎng)絡(luò)IO 模型

2021-11-09 06:01:35

前端JITAOT

2024-11-04 00:00:03

viewportDOMSPA

2021-02-08 23:47:51

文件存儲(chǔ)塊存儲(chǔ)對(duì)象存儲(chǔ)

2020-11-11 07:32:18

MySQL InnoDB 存儲(chǔ)

2023-04-11 15:57:49

JavaScriptCSSHTML

2019-11-21 14:22:12

WiFiWLAN區(qū)別

2021-02-14 22:33:23

Java字符字段

2021-11-01 13:10:48

私有云混合云行業(yè)云
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)