C#中IEnumerable接口用法实例分析
作者:heishui 发布时间:2022-03-24 12:40:21
本文实例讲述了C#中IEnumerable接口用法。分享给大家供大家参考。具体分析如下:
枚举数可用于读取集合中的数据,但不能用于修改基础集合。
最初,枚举数定位在集合中第一个元素前。Reset 方法还会将枚举数返回到此位置。在此位置上,Current 属性未定义。因此,在读取 Current 的值之前,必须调用 MoveNext 方法将枚举数提前到集合的第一个元素。
在调用 MoveNext 或 Reset 之前,Current 返回同一对象。MoveNext 将 Current 设置为下一个元素。
如果 MoveNext 越过集合的末尾,枚举数就会被放置在此集合中最后一个元素的后面,且 MoveNext 返回 false。当枚举数位于此位置时,对 MoveNext 的后续调用也返回 false。如果对 MoveNext 的最后一次调用返回 false,则 Current 为未定义。若要再次将 Current 设置为集合的第一个元素,可以调用 Reset,然后再调用 MoveNext。
只要集合保持不变,枚举数就保持有效。如果对集合进行更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,而且其行为是不确定的。
枚举数没有对集合的独占访问权;因此,从头到尾对一个集合进行枚举在本质上不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。
下面的代码示例演示如何实现自定义集合的 IEnumerable 接口。在此示例中,没有显式调用但实现了 GetEnumerator,以便支持使用 foreach(在 Visual Basic 中为 For Each)。此代码示例摘自 IEnumerable 接口的一个更大的示例。
using System;
using System.Collections;
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;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
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 App
{
static void Main()
{
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);
}
}
/* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*/
希望本文所述对大家的C#程序设计有所帮助。
猜你喜欢
- ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContex
- 一、目的针对不同地区,设置不同的语言信息。SpringBoot国际化配置文件默认放在classpath:message.properties
- 前几天在琢磨mybatis xml热加载的问题,原理还是通过定时扫描xml文件去跟新,但放到项目上就各种问题,由于用了mybatisplus
- 一、Spring Boot 、 Spring MVC 、Spring对比首先你需要明白一件事情:Spring Boot项目目的并不是替换Sp
- 日期和时间格式由 日期和时间模式字符串 指定。在 日期和时间模式字符串 中,未加引号的字母 'A' 到 'Z'
- java中this与super关键字的使用方法这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各
- Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。自从5.0开始,jdk在java.util.concurrent
- import java.io.UnsupportedEncodingException;import java.security.Inval
- 使用限制JDBC未支持列表Sharding-JDBC暂时未支持不常用的JDBC方法。DataSource接口不支持timeout相关操作Co
- 前言在 C# 编程中,管道式编程(Pipeline Style programming)其实存在已久,最明显的就是我们经常使用的 LINQ。
- 本文实例讲述了Android编程实现下载时主界面与详细界面一致更新的方法。分享给大家供大家参考,具体如下:1、创建监听管理类public c
- 这篇文章主要介绍了如何使用HttpClient发送java对象到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 使用ManagedWifi查看当前Wifi信号并选择wifiusing System;using System.Collections.Ge
- MyBatis if test 判断字符串相等不生效采用 MyBatis 框架操作 MySQL 数据库时,判断传入的字符串 priceFla
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 实体类时间格式化java 实体类 时间格式化注解@JsonFormat(pattern = "yyyy-MM-dd HH:mm:s
- 大家好,这是 [C#.NET 拾遗补漏] 系列的第 07 篇文章。在 C# 中,大多数方法都是通过 return 语句立即把程序的控制权交回
- Android5.0之后提供了JobService和JobScheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务
- 承接上文 传送门一.完善登录功能按照常理,只有登陆过后才能进入首页,若没有登陆则应当直接跳转到登陆页面,这样的场景不就完美契合过滤器的功效吗
- 上一小节简单分析了directArena内存分配大概流程 ,知道其先命中缓存, 如果命中不到, 则区分配一款连续内存, 这一小节带