System.Collections.Generic.IEnumerable<T>接口
2013-09-12 09:34
447 查看
System.Collections.Generic.IEnumerable<T>接口
/*要实现System.Collections.Generic.IEnumerable<T>接口比较麻烦,这个接口有一个方法:GetEnumerator(),不过有两个重载版本* 第一:System.Collections.Generic.IEnumerator<T> GetEnumerator(),
* 第二:System.Collections.IEnumerator GetEnumerator()...(注意这里的重载是以返回值为区别的,其实不然,这只是接口而已,还没有去实现).
* 那么如何去实现这个方法呢?
* 如下:必须同时有方法一、方法二,缺一不可.
* 如果缺了方法一,编译错误:不实现接口成员"System.Collections.Generic.IEnumerable<string> GetEnumerator()",
* 如果缺了方法二,编译错误:不实现接口成员"System.Collections.IEnumerable GetEnumerator()",因为它没有匹配的返回类型
* "System.Collections.IEnumerator",所以两个方法缺一不可,而且方法二必须是显式实现接口,而不是像方法三那样隐式
* 实现接口,如果像方法三那样写,编译错误:已定义了一个名为"GetEnumerator"的具有相同参数类型的成员,即这违反
* 了重载的规则,所以必须显式实现接口,显式实现接口可以避免由于方法名和参数相同而引起的重载问题。值得一提
* 的是,.NET中仅有返回值类型不同的重载方法范例是implicit,explicit自定义类型转换中的内容,详见书,CLR支持这种仅
* 有返回值不同的重载,但并不被C#这样的高级语言所支持 * 另外值得一提的是:泛型技术,使得相同的参数列表、相同的返回值类型的情况也可以构成重载(如下MyFunc方法)
*/
public class UserInfo : System.Collections.Generic.IEnumerable<string>
{
string[] users = { "wang", "zhang", "li" };
//方法一
public System.Collections.Generic.IEnumerator<string> GetEnumerator()
{
for (int i = 0; i < users.Length; i++)
{
yield return users[i];
}
yield break;
yield return "BREAK";
}
//方法二
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();//IEnumerator<T>继承自IEnumerator
}
//方法三
//public System.Collections.IEnumerator GetEnumerator()
//{
// return GetEnumerator();
//}
public bool MyFunc<T>(string str, int id)
{
IList < T > temp = new List<T>();
if (temp != null)
return true;
else
return false;
}
public bool MyFunc(string str, int id)
{
return true;
}
public IList<string> MyList()
{
return null;
}
//public IList MyList()
//{
// return null;
//}
}
补充几个概念:
枚举器:在C#中,一个对象只要实现System.Collections.IEnumerator或者System.Collections.Generic.IEnumerator<T>(继承自IEnumerator)接口,那么这个对象就是一个枚举器。
可枚举对象:是一个实现了System.Collections.IEnumerable或System.Collections.Generic.IEnumerable<T>(继承自IEnumerable)的对象。
迭代器就是用于生成一个既是枚举器又是可枚举对象的。
在.NET中,覆写实现了运行时的多态性,二重载实现了编译时的多态性
相关文章推荐
- “System.Collections.Generic.IEnumerable<TestDAL.HistoryData>”不包含“ToList”的定义
- “System.Collections.Generic.IEnumerable<TestDAL.HistoryData>”不包含“ToList”的定义
- 无法将类型“System.Collections.Generic.IEnumerable<EmailSystem.Model.TemplateInfo>”隐式转换为“System.Collections.Generic.List<EmailSystem.Model.TemplateInf
- “System.Collections.Generic.IEnumerable<decimal>”不包含“ToArray”的定义,并且找不到可接受类型为“System.Collections.Generic.IEnumerable<decimal>”的第一个参数的扩展方法“ToArray”
- 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要 1 个类型参数
- 使用泛型类型System.Collections.Generic.Icomparer &lt;T&gt; 需要一个类型参数
- [corefx注释说]-System.Collections.Generic.Stack<T>
- [corefx注释说]-System.Collections.Generic.Queue<T>
- NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>
- IComparable<>,IFormattable,IEnumerable<>接口使用
- 一图胜千言 – System.Linq.IEnumerable<T> vs System.Linq.IQuerable<T>
- IEqualityComparer<T> VS System.IEquatable<T>
- 一个提醒...such as implement System.Collections.Generic.IComparer<T>
- MVC 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc3Modeltest.Models.Movie>”。存在一个显式转换(是否缺少强制转换?))
- C#编译问题'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument
- ForEach :对 System.Collections.Generic.List<T> 的每个元素执行指定操作
- 用泛型的IEqualityComparer<T>接口去重复项
- 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”
- 在Share Point 2010中,如何使用 in 语句?并且解决Boolean Contains[Int32](System.Collections.Generic.IEnumerable`1[System.Int32], Int32)的问题。
- 《学习笔记》System.Collections.Generic 命名空间 HashSet<T>