代码之家  ›  专栏  ›  技术社区  ›  Display Name Camilo Terevinto

重构接口方法——提取接口方法的代码片段以创建新的包装方法——是最佳实践吗?

c#
  •  -1
  • Display Name Camilo Terevinto  · 技术社区  · 16 年前

    IEnumerator IEnumerable.GetEnumerator()
    {
       return (IEnumerator) GetEnumerator();
    }
    
    public PeopleEnum GetEnumerator()
    {
        return new PeopleEnum(_people);
    }
    

    作者为什么这样做?这是最佳实践吗?

    --------编辑---------

    http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx

    为什么作者没有做如下的事情:

     IEnumerator IEnumerable.GetEnumerator()
     {
            return new PeopleEnum(_people);
     }
    

    =======第二次编辑==========

    using System;
    using System.Collections;
    
    namespace MyEnumerableEnumerator
    {
        class Person
        {
            public Person(string name, int age)
            {
                Name = name;
                Age = age;
            }
    
            public string Name { get; set; }
            public int Age { get; set; }
        }
    
        class PeopleEnumerator : IEnumerator
        {
            int Position { get; set; }
            Person[] People { get; set; }
            int Length { get; set; }
    
            public PeopleEnumerator(Person[] people)
            {
                People = people;
                Position = -1;
                Length = people.Length;
            }
    
            public bool MoveNext()
            {
                Position++;
                return Position < Length;
            }
    
            public void Reset()
            {
                Position = -1;
            }
    
            public object Current
            {
                get
                {
                    try
                    {
                        return People[Position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }
    
        class People : IEnumerable
        {
            Person[] Persons { get; set; }
    
            public People(Person[] people)
            {
                Persons = new Person[people.Length];
    
                for (int x = 0; x < people.Length; x++)
                    Persons[x] = people[x];
            }
    
            //public IEnumerator GetEnumerator()
            //{
            //    return new PeopleEnumerator(Persons);
            //}
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
    
            private IEnumerator GetEnumerator()
            {
                return new PeopleEnumerator(Persons);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
    
                Person[] persons = { new Person("Mark", 1), new Person("Siva", 2) };
    
                People people = new People(persons);
    
                foreach (Person person in people)
                {
                    Console.WriteLine("Name: {0}, Age: {1}", person.Name, person.Age);
                }
    
            }
        }
    }
    
    3 回复  |  直到 16 年前
        1
  •  1
  •   jgauffin    16 年前

    他们之所以这样做是因为他们需要一个非泛型集合的类型化迭代器。

    编辑

    使用更改的代码尝试此操作:

            var enumerator = peopleList.GetEnumerator();
            while (enumerator.MoveNext())
                Console.WriteLine(enumerator.Current.firstName); //compile error.
    

    它不会编译,您必须将枚举数强制转换为PeopleEnum或更改回返回类型。

    您可以在这里阅读foreach的隐式类型转换:

    http://www.dev102.com/2009/07/28/pay-attention-to-the-foreach-implicit-casting/

    http://csharpindepth.com/ViewNote.aspx?NoteID=1

    http://msdn.microsoft.com/en-us/library/bb308966.aspx

    向下滚动到26.1中的最后一节

        2
  •  1
  •   Siva Gopal    16 年前

    相反,您只需将代码更改为:

    IEnumerator IEnumerable.GetEnumerator()
       {
          return new PeopleEnum(_people);
       }
    

    public IEnumerator GetEnumerator()
    {
       return new PeopleEnum(_people);
    }
    

    因为IEnumerable接口在那里,所以我们不能忽略GetEnumerator()方法的实现。

        3
  •  0
  •   µBio    16 年前