[英]Whats there besides Explicit interface implementation?
考慮示例:
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];
}
}
// Explicit Interface implementation
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
}
// What is this? Its neither overloading nor over riding.. What else is it?
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
我已經看過這種例子了..但是無法弄清楚它到底是什么嗎? 請幫助
它只是在類上定義一個方法。 並非每個類方法都必須是接口方法的實現。
這只是另一個類方法。
話雖如此,在實現IEnumerable
時這並不是一件很普通的事情。 在當前框架中,更好的方法是直接實現IEnumerable<Person>
並返回IEnumerable<Person>
。
這將提供一個更清潔的API,如預期的用法,並且它將與當前的框架方法一起工作(即:所有LINQ都無需使用.Cast<T>
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.