IEnumerable<T> 接口

.NET Framework 4.6 and 4.5
 

公开枚举数,该枚举数支持在指定类型的集合上进行简单迭代。

若要浏览此类型的.NET Framework 源代码,请参阅参考源

命名空间:  System.Collections.Generic
程序集:  mscorlib(在 mscorlib.dll 中)

 
 
 
public interface IEnumerable<out T> : IEnumerable

类型参数

   out T

要枚举的对象的类型。

此类型参数是协变。即可以使用指定的类型或派生程度更高的类型。有关协变和逆变的详细信息,请参阅 泛型中的协变和逆变

IEnumerable<T> 类型公开以下成员。

方法

显示: 继承 保护
  名称 描述
GetEnumerator 返回一个循环访问集合的枚举器。

GetEnumerator 详解

返回一个循环访问集合的枚举器。

命名空间:   System.Collections.Generic
程序集:  mscorlib(mscorlib.dll 中)

语法  

IEnumerator<T> GetEnumerator()

返回值

Type: System.Collections.Generic.IEnumerator<T>

用于循环访问集合的枚举数。

备注

 
 

返回IEnumerator<T>提供了通过公开来循环访问集合的能力Current属性。读取集合中的数据,但不是能修改集合,您可以使用枚举器。

最初,枚举数位于集合中的第一个元素之前。在此位置上,Current是不确定的。因此,您必须调用MoveNext方法使枚举器前进到之前读取值的集合的第一个元素Current

Current返回同一个对象,直到MoveNext作为再次调用MoveNext设置Current到下一个元素。

如果MoveNext越过集合,该枚举数的末尾将被定位在集合中的最后一个元素之后和MoveNext返回false。当枚举数位于此位置上,对后续调用MoveNext也会返回false。如果最后一次调用到MoveNext返回falseCurrent是不确定的。您不能设置Current再次为集合的第一个元素必须改为创建新的枚举器实例。

一个枚举器没有对集合的独占访问,因此,只要集合保持不变,枚举器保持有效。如果进行了更改到集合中,如添加、 修改,或删除元素),则枚举器将失效,并可能会收到意外的结果时。此外,对集合进行枚举不是线程安全过程。若要保证线程安全,您应枚举期间锁定集合,或实现同步对集合。

集合中的默认实现System.Collections.Generic命名空间时不同步。

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq; public class App
{
// Excercise the Iterator and show that it's more
// performant.
public static void Main()
{
TestStreamReaderEnumerable();
Console.WriteLine("---");
TestReadingFile();
} public static void TestStreamReaderEnumerable()
{
// Check the memory before the iterator is used.
long memoryBefore = GC.GetTotalMemory(true);
IEnumerable<String> stringsFound;
// Open a file with the StreamReaderEnumerable and check for a string.
try {
stringsFound =
from line in new StreamReaderEnumerable(@"c:\temp\tempFile.txt")
where line.Contains("string to search for")
select line;
Console.WriteLine("Found: " + stringsFound.Count());
}
catch (FileNotFoundException) {
Console.WriteLine(@"This example requires a file named C:\temp\tempFile.txt.");
return;
} // Check the memory after the iterator and output it to the console.
long memoryAfter = GC.GetTotalMemory(false);
Console.WriteLine("Memory Used With Iterator = \t"
+ string.Format(((memoryAfter - memoryBefore) / ).ToString(), "n") + "kb");
} public static void TestReadingFile()
{
long memoryBefore = GC.GetTotalMemory(true);
StreamReader sr;
try {
sr = File.OpenText("c:\\temp\\tempFile.txt");
}
catch (FileNotFoundException) {
Console.WriteLine(@"This example requires a file named C:\temp\tempFile.txt.");
return;
} // Add the file contents to a generic list of strings.
List<string> fileContents = new List<string>();
while (!sr.EndOfStream) {
fileContents.Add(sr.ReadLine());
} // Check for the string.
var stringsFound =
from line in fileContents
where line.Contains("string to search for")
select line; sr.Close();
Console.WriteLine("Found: " + stringsFound.Count()); // Check the memory after when the iterator is not used, and output it to the console.
long memoryAfter = GC.GetTotalMemory(false);
Console.WriteLine("Memory Used Without Iterator = \t" +
string.Format(((memoryAfter - memoryBefore) / ).ToString(), "n") + "kb");
}
} // A custom class that implements IEnumerable(T). When you implement IEnumerable(T),
// you must also implement IEnumerable and IEnumerator(T).
public class StreamReaderEnumerable : IEnumerable<string>
{
private string _filePath;
public StreamReaderEnumerable(string filePath)
{
_filePath = filePath;
} // Must implement GetEnumerator, which returns a new StreamReaderEnumerator.
public IEnumerator<string> GetEnumerator()
{
return new StreamReaderEnumerator(_filePath);
} // Must also implement IEnumerable.GetEnumerator, but implement as a private method.
private IEnumerator GetEnumerator1()
{
return this.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator1();
}
} // When you implement IEnumerable(T), you must also implement IEnumerator(T),
// which will walk through the contents of the file one line at a time.
// Implementing IEnumerator(T) requires that you implement IEnumerator and IDisposable.
public class StreamReaderEnumerator : IEnumerator<string>
{
private StreamReader _sr;
public StreamReaderEnumerator(string filePath)
{
_sr = new StreamReader(filePath);
} private string _current;
// Implement the IEnumerator(T).Current publicly, but implement
// IEnumerator.Current, which is also required, privately.
public string Current
{ get
{
if (_sr == null || _current == null)
{
throw new InvalidOperationException();
} return _current;
}
} private object Current1
{ get { return this.Current; }
} object IEnumerator.Current
{
get { return Current1; }
} // Implement MoveNext and Reset, which are required by IEnumerator.
public bool MoveNext()
{
_current = _sr.ReadLine();
if (_current == null)
return false;
return true;
} public void Reset()
{
_sr.DiscardBufferedData();
_sr.BaseStream.Seek(, SeekOrigin.Begin);
_current = null;
} // Implement IDisposable, which is also implemented by IEnumerator(T).
private bool disposedValue = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
// Dispose of managed resources.
}
_current = null;
if (_sr != null) {
_sr.Close();
_sr.Dispose();
}
} this.disposedValue = true;
} ~StreamReaderEnumerator()
{
Dispose(false);
}
}
// This example displays output similar to the following:
// Found: 2
// Memory Used With Iterator = 33kb
// ---
// Found: 2
// Memory Used Without Iterator = 206kb
 

IEnumerable<T> 接口和GetEnumerator 详解的更多相关文章

  1. c# WebApi之接口返回类型详解

    c# WebApi之接口返回类型详解 https://blog.csdn.net/lwpoor123/article/details/78644998

  2. “全栈2019”Java第六十四章:接口与静态方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  3. “全栈2019”Java第六十三章:接口与抽象方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  4. “全栈2019”Java第六十二章:接口与常量详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  5. LWIP network interface 即 LWIP 的 硬件 数据 接口 移植 首先 详解 STM32 以太网数据 到达 的第一站: ETH DMA 中断函数

    要 运行  LWIP  不光 要实现  OS  的 一些 接口  ,还要 有 硬件 数据 接口 移植 ,即 网线上 来的 数据 怎么个形式 传递给  LWIP ,去解析 做出相应的 应答  ,2017 ...

  6. 抽象类和接口的区别详解、package和import

    1.抽象类和接口以及抽象类和接口的区别. 1.1.抽象类的基础语法(见昨天笔记) 1.2.接口的基础语法 1.接口是一种"引用数据类型". 2.接口是完全抽象的. 3.接口怎么定义 ...

  7. Java接口修饰符详解

    接口就是提供一种统一的”协议”,而接口中的属性也属于“协议”中的成员.它们是公共的,静态的,最终的常量.相当于全局常量.抽象类是不“完全”的类,相当于是接口和具体类的一个中间层.即满足接口的抽象,也满 ...

  8. python接口自动化(三)--如何设计接口测试用例(详解)

    简介 上篇我们已经介绍了什么是接口测试和接口测试的意义.在开始接口测试之前,我们来想一下,如何进行接口测试的准备工作.或者说,接口测试的流程是什么?有些人就很好奇,接口测试要流程干嘛?不就是拿着接口文 ...

  9. Spring钩子方法和钩子接口的使用详解

    本文转自:http://www.sohu.com/a/166804449_714863 前言 SpringFramework其实具有很高的扩展性,只是很少人喜欢挖掘那些扩展点,而且官方的Refrenc ...

随机推荐

  1. Tfs 2015 代理池配置笔记

    Tfs的构建代理池其实是在代理服务器上开启一个TFSBuild的代理服务,配好相关的Tfs地址后,就能在Tfs管理界面看到了. 如果是Tfs服务和发布代理是同一台服务器,具体操作详见: 安装TFS20 ...

  2. Git版本控制工具安装与配置

    这里太多,我写在这里方便复制: sudo yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel curl-dev ...

  3. stm8s 时钟库函数选择内部RC初始化

    //本文选择16M内部RC震荡.分频为1 即系统时钟为16M void CLK_HSICmd(FunctionalState NewState) { /* Check the parameters * ...

  4. Spark Shuffle(二)Executor、Driver之间Shuffle结果消息传递、追踪(转载)

    1. 前言 在博客里介绍了ShuffleWrite关于shuffleMapTask如何运行,输出Shuffle结果到Shuffle_shuffleId_mapId_0.data数据文件中,每个exec ...

  5. 用ildasm/ilasm修改IL代码(操作步骤)

    在开发中遇到这样一个场景,需要修改一个dll文件(.NET程序集)中某些地方的类型名称,但没有源代码,只能修改IL代码. 操作步骤如下: 1. 运行ildasm ildasm是由微软提供的.NET程序 ...

  6. (转)通过HTTP RESTful API 操作elasticsearch搜索数据

    样例数据集 这是编造的JSON格式银行客户账号信息文档,文档schema如下: { “account_number”: 0, “balance”: 16623, “firstname”: “Brads ...

  7. 7.2 Models -- Defining Models

    一.概述 1. 模型是一个类,它定义了你呈现给用户的数据的属性和行为.用户希望如果他们离开你的应用程序,并返回后(或如果他们刷新页面)看到的任何东西应该被一个model代表. 2. 确保在ember. ...

  8. addslashes — 使用反斜线引用字符串

    返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线.这些字符是单引号(').双引号(").反斜线(\)与 NUL( NULL 字符). 一个使用 addslashes() ...

  9. JavaScript在页面中的引用方法

    现在前端开发越来越流行,框架也越来越多,像ExtJs.JQuery.Bootstrap等.虽然入行这么多年,但是感觉自己在前端方面还是存在基础不牢的地方,特别是CSS和JS.因此最近打算重新阅读这方面 ...

  10. Spring—spring概述

    Spring框架的特点? 1:轻量级,一站式开发 2:易用,追求代码的最佳实现 3:Spring的内容: a:Ioc容器 b:AOP实现 c:数据访问支持(ORM框架/声明事务[Transaction ...