首页 / 知识
关于c#:我可以使用返回IEnumerator < T >的方法并将其用于foreach循环中吗?
2023-04-11 17:49:00

Can I have a method returning IEnumerator
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private static IEnumerator<TextBox> FindTextBoxes(Control rootControl) { foreach (Control control in rootControl.Controls) { if (control.Controls.Count > 0) { // Recursively search for any TextBoxes within each child control foreach (TextBox textBox in FindTextBoxes(control)) { yield return textBox; } } TextBox textBox2 = control as TextBox; if (textBox2 != null) { yield return textBox2; } } } |
像这样使用它:
1 2 3 4 | foreach(TextBox textBox in FindTextBoxes(this)) { textBox.Height = height; } |
但是,当然编译器会吐出它的虚拟对象,因为foreach期望使用IEnumerable而不是IEnumerator。
有没有一种方法可以不必使用GetEnumerator()方法创建单独的类?
正如编译器告诉您的那样,您需要将返回类型更改为IEnumerable。 这就是yield return语法的工作方式。
只是为了澄清
1 | private static IEnumerator<TextBox> FindTextBoxes(Control rootControl) |
更改为
1 | private static IEnumerable<TextBox> FindTextBoxes(Control rootControl) |
那应该是全部:-)
如果返回IEnumerator,则每次调用该方法都将是一个不同的枚举器对象(就像在每次迭代中重置枚举器一样)。 如果返回IEnumerable,则可以使用yield语句基于该方法枚举foreach。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // Generic function that gets all child controls of a certain type, // returned in a List collection private static List< T > GetChildTextBoxes< T >(Control ctrl) where T : Control{ List< T > tbs = new List< T >(); foreach (Control c in ctrl.Controls) { // If c is of type T, add it to the collection if (c is T) { tbs.Add((T)c); } } return tbs; } private static void SetChildTextBoxesHeight(Control ctrl, int height) { foreach (TextBox t in GetChildTextBoxes<TextBox>(ctrl)) { t.Height = height; } } |
如果为您提供了枚举器,并且需要在for-each循环中使用它,则可以使用以下代码对其进行包装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | static public class enumerationHelper { public class enumeratorHolder< T > { private T theEnumerator; public T GetEnumerator() { return theEnumerator; } public enumeratorHolder(T newEnumerator) { theEnumerator = newEnumerator;} } static enumeratorHolder< T > toEnumerable< T >(T theEnumerator) { return new enumeratorHolder< T >(theEnumerator); } private class IEnumeratorHolder< T >:IEnumerable< T > { private IEnumerator< T > theEnumerator; public IEnumerator< T > GetEnumerator() { return theEnumerator; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return theEnumerator; } public IEnumeratorHolder(IEnumerator< T > newEnumerator) { theEnumerator = newEnumerator; } } static IEnumerable< T > toEnumerable< T >(IEnumerator< T > theEnumerator) { return new IEnumeratorHolder< T >(theEnumerator); } } |
最新内容
相关内容
python如何退出循环
python如何退出循环,培训,代码,语句,条件,嵌套,序列,剩余,最小,以上,语言,break语句pythonbreak语句,就像在C语言中,打破了最小封闭for或whilepython怎么调用类方法
python怎么调用类方法,培训,方法,实例,静态,定义,参数,属性,对象,关键字,以上,python中的类用来描述具有相同的属性和方法的对象的集合。它定pythonfor循环语句怎么写
pythonfor循环语句怎么写,数据,培训,项目,语句,循环体,实例,教程,时会,字符串,序列,pythonfor循环可以遍历任何序列的项目,如一个列表或者一个python判断字符串是否为小数的方法
python判断字符串是否为小数的方法,培训,代码,合法,小数点,小数,整数,字符串,方法,右边,左边,python想判断一个字符串是不是一个合法的小数,但python可以继承父类方法吗
python可以继承父类方法吗,培训,代码,名字,方法,动物,属性,编译器,这样的话,定义,里面,python继承,调用父类属性方法在python里面,继承一个类python函数的高级使用方法
python函数的高级使用方法,代码,函数,灵活,数据,培训,时间,定义,程序,赋值,变量,Python的函数是“一等公民”,因此函数本身也是一个对象,函数既python私有方法是什么
python私有方法是什么,培训,公开,代码,方法,属性,变量,函数,前面,成员,法名,Python默认的成员函数和成员变量都是公开的,Python私有属性和方法python字符串连接的方法有哪些
python字符串连接的方法有哪些,培训,字符串,基础,方式,两个,变量,函数,空白,语言,功能,python中有很多字符串连接方式,下面总结一下:最原始的字python怎么写循环
python怎么写循环,培训,语句,条件,双数,表达式,详解,两者,命令,下面,次数,python中的循环语句,可使用for循环,或者while循环。两者的区别是,forpython字符串拼接有哪些方法?
python字符串拼接有哪些方法?,培训,一致,字符串,位置,异常,结果,方式,方法,字符,参数,python拼接字符串一般有以下几种方法:①直接通过(+)操作python创建多线程的两种方法
python创建多线程的两种方法,培训,第一,代码,业务,方法,线程,函数,任务,演示,实例,当我们使用python编程的过程中需要多个输出的任务的话,为了python实例方法的使用注意
python实例方法的使用注意,培训,实例,方法,对象,定义,以上,参数,更多,内容,python实例方法的使用注意1、实例方法是从属于实例对象的方法,定