簡體   English   中英

除了顯式接口實現之外還有什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM