網站首頁 編程語言 正文
簡介
IEnumerable接口是非常的簡單,只包含一個抽象的方法GetEnumerator(),它返回一個可用于循環訪問集合的IEnumerator對象。對于所有數組的遍歷,都來自IEnumerable接口。
IEnumerator對象有什么呢?它是一個真正的集合訪問器,沒有它,就不能使用foreach語句遍歷集合或數組,因為只有IEnumerator對象才能訪問集合中的項,假如連集合中的項都訪問不了,那么進行集合的循環遍歷是不可能的事情了。
一、foreach在IEnumerable中案例
public static void Test3()
{
MyInt temp = new MyInt();
foreach (int item in temp)
Console.WriteLine(item);
}
//foreach的必須要實現IEnumerable和IEnumerator的接口
public class MyInt : IEnumerable
{
int[] temp = { 1, 32, 43, 343 };
public IEnumerator GetEnumerator()
{
return temp.GetEnumerator();
}
}
相當于下面代碼:
public static void Test1()
{
int[] myArray = { 1, 32, 43, 343 };
//獲取要遍歷的枚舉數
IEnumerator myie = myArray.GetEnumerator();
//重置當前項,相當于把指針移到初始位置:position = -1; 一開始認識數組的索引從“0”開始
myie.Reset();
//向前移動一個索引,返回Bool類型,判斷是否超出下標
while (myie.MoveNext())
{
int i = (int)myie.Current;//從Object轉成對應類型
Console.WriteLine("Value: {0}", i);
}
}
包含一個屬性兩個方法
MoveNext:把當前的項移動到下一項(類似于索引值),返回一個bool值,這個bool值用來檢查當前項是否超出了枚舉數的范圍!
Current:獲取當前項的值,返回一個object的類型!
Reset:顧名思義也就是把一些值恢復為默認值,比如把當前項恢復到默認狀態值!
二、Lamda在IEnumerable中案例
//lamda表達式在數組中查詢
public static void Test2()
{
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
//List<string> query = fruits.Where(fruit => fruit.Length < 6).ToList();
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
foreach (string fruit in query)
Console.WriteLine(fruit);
}
只篩選出List中的元素長度小于6的值,然后打印出。
實現自定義集合的 IEnumerable和IEnumerator 接口
namespace ConsoleApplication1
{
//定義Person類
public class Person
{
//初始化
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
//類成員
public string firstName;
public string lastName;
}
//實現接口
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
//獲取枚舉數
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
//向下推移索引,返回Bool類型值
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
//重置默認索引位置,默認下標為0
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
//當前索引值
public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class Program
{
static void Main(string[] args)
{
//實例化Person
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
}
}
}
原文鏈接:https://www.cnblogs.com/wml-it/p/14777098.html
相關推薦
- 2022-01-27 @ConfigurationProperties放在類上跟放在方法上有什么區別
- 2022-09-30 oracle表空間不足ORA-01653的問題:?unable?to?extend?table_or
- 2022-12-05 Python異常?ValueError的問題_python
- 2023-07-22 使用log4j2為日志增加代碼行號
- 2022-09-28 基于OpenCV(python)的實現文本分割之垂直投影法_python
- 2022-09-28 OpenCV(python)版實現文本分割之水平投影法_python
- 2023-07-04 JUC 之CountDownLatch工具類
- 2022-11-27 Unity?數據存儲和讀取的方法匯總_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支