排名
1
文章
860
粉丝
112
评论
163
.net core自定义项目模板,创建自己的模板项目,使用命令行创建模板项目
尘叶心繁 : 可以可以讲真的我都想弄个模板
net core webapi post传递参数
庸人 :
确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 : 已精
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 :
疯狂反射
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

IEnumerable:可枚举。
IEnumerator:一种枚举方案。
如果你想使用枚举,就必须实现接口IEnumerable的方法GetEnumerator(),但是这个方法
返回类型是IEnumerator,他才是真正的实现枚举的功臣,因此要枚举的容器就必须实现
IEnumerator的方法和属性。IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。
微软官方的一个例子:
- 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);
-
- }
- }
People 实现 IEnumerable ,说明People是可以枚举的,但是怎么枚举呢?
PeopleEnum 实现 IEnumerator ,真正的实现了枚举。他们通过GetEnumerator建立联系。
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价