没有实现IEnumerable的情况下支持foreach的一个例子 [英] An example for supporting foreach without implementing IEnumerable

查看:328
本文介绍了没有实现IEnumerable的情况下支持foreach的一个例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此博客这就解释了在不执行 IEnumerable 的情况下,可以支持 foreach 。但是并没有详细介绍这个实现。

I am looking at this blog which explains that foreach can be supported without implementing IEnumerable. But does not go into the details of the implementation.

我正在寻找一个如何支持 foreach 而不执行 IEnumerable

I am looking for an example of how to support foreach without implementing IEnumerable.

编辑:感谢@Sam我的评论,我得到了我正在寻找的东西。 (见下面的答案)

Thanks to @Sam I am 's comment, I got what I was looking for. (see my answer below)

推荐答案

感谢@Sam我的评论,我能够调整上的代码<一个href =http://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v=vs.100%29.ASPX =nofollow>这个页面,一起下面,不使用IEnumerable或IEnumerator:

Thanks to @Sam I am 's comment, I am able to tweak the code on this page and put together the following, without using IEnumerable or IEnumerator:

   //Person Object
   public class Person
   {
        public Person(string fName, string lName)
        {
            this.firstName = fName;
            this.lastName = lName;
        }
    string firstName;
    public string lastName;
   }

   //****Object Collection. 
   //****This class usually needs to implement IEnumerable 
   //****But we are avoiding that here.
   public class People
   {
      private Person[] _people;
      public People(Person[] pArray)
        {
           _people = new Person[pArray.Length];    
           for (int i = 0; i < pArray.Length; i++)
           {
              _people[i] = pArray[i];
           }
         }

    public PeopleEnumSimulator GetEnumerator()
    {
        return new PeopleEnumSimulator(_people);
    }

    public class PeopleEnumSimulator
    {
        public Person[] _people;

        int position = -1;

        public PeopleEnumSimulator(Person[] list)
        {
            _people = list;
        }

        public bool MoveNext()
        {
            position++;
            return (position < _people.Length);
        }

        public void Reset()
        {
            position = -1;
        }

        public Person Current
        {
            get
            {
                try
                {
                    return _people[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
}   


//****Now, Use the Foreach
    Person[] myPeople = new Person[3]
    {
        new Person("John", "Smith"),
        new Person("Jim", "Johnson"),
        new Person("Sue", "Rabon"),
    };
    People peopleList = new People(myPeople);
    foreach (Person p in peopleList)
        Response.Write(p.firstName + " " + p.lastName);





//****************************************
//******** A Generic Implementation********
public class People<T>
{
    private T[] _people;
    public People(T[] pArray)
    {
        _people = new T[pArray.Length];

        for (int i = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }
    }

    public PeopleEnumSimulator GetEnumerator()
    {
        return new PeopleEnumSimulator(_people);
    }

    public class PeopleEnumSimulator
    {
        public T[] _people;

        int position = -1;

        public PeopleEnumSimulator(T[] list)
        {
            _people = list;
        }

        public bool MoveNext()
        {
            position++;
            return (position < _people.Length);
        }

        public void Reset()
        {
            position = -1;
        }

        public T Current
        {
            get
            {
                try
                {
                    return _people[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
}

       // use the foreach
       //For Type Person
            People<Person> peopleList = new People<Person>(myPeople);

            foreach (Person p in peopleList)
                Response.Write(p.firstName + " " + p.lastName);

            //break
            Response.Write(" </br></br></br>       ");

            //For Type Int
            int[] n3 = { 2, 4, 6, 8 };

            People<int> intList = new People<int>(n3);

            foreach (int p in intList)
                Response.Write(p);

这篇关于没有实现IEnumerable的情况下支持foreach的一个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆