涉及 C#的 foreach问题

当时是用foreach实现遍历,但是函数传入参数是Object类型的,由于Objectl类型没有实现相关接口,所以foreach并不能执行。

那么下面我们来看看,想要使用foreach需要具备什么条件。

需要实现IEnumerable接口或声明GetEnumerator方法的类型。

 下面我们来看看foreach原理,

参考原文  http://blog.csdn.net/guobin_lu/article/details/8812092

为什么有些类可以可以用foreach遍历,有些类却不可以了.经反编译过后得出:

 

 

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Collections;  
      
    namespace Myforeach  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {        
                Person p = new Person();  
                p[0] = "宝马";  
                p[1] = "奥迪";  
                p[2] = "阿斯顿马丁";  
                //for (int i = 0; i < p.Count; i++)  
                //{  
                //    Console.WriteLine(p[i]);  
                //}  
      
                //任何类型,只要想使用foreach来循环遍历,就必须在当前类型中  
                //存在: public IEnumerator GetEnumerator()方法,(一般情况我们会通过实现IEnumerable接口,来创建该方法。)  
                foreach (string item in p)  
                {  
                    Console.WriteLine(item);  
                }  
      
                //IEnumerator etor = p.GetEnumerator();  
                //while (etor.MoveNext())  
                //{  
                //    string str = etor.Current.ToString();  
                //    Console.WriteLine(str);  
                //}  
                Console.ReadKey();  
      
      
            }  
        }  
      
        public class Person : IEnumerable  
        {  
            private List<string> listCar = new List<string>();  
      
            public int Count  
            {  
                get  
                {  
                    return this.listCar.Count;  
                }  
      
            }  
      
            public string this[int index]  
            {  
                get  
                {  
                    return listCar[index];  
                }  
      
                set  
                {  
                    if (index >= listCar.Count)  
                    {  
                        listCar.Add(value);  
                    }  
                    else  
                    {  
                        listCar[index] = value;  
                    }  
                }  
            }  
            public string Name  
            {  
                get;  
                set;  
            }  
            public int Age  
            {  
                get;  
                set;  
            }  
            public string Email  
            {  
                get;  
                set;  
            }  
     
            #region IEnumerable 成员  
      
            //这个方法的作用不是用来遍历的,而是用来获取一个对象  
            //这个对象才是用来遍历的。  
            public IEnumerator GetEnumerator()  
            {  
                return new PersonEnumerator(listCar);  
            }  
     
            #endregion  
        }  
      
        //这个类型,的作用就是用来遍历Person中的List集合的。  
        public class PersonEnumerator : IEnumerator  
        {  
            public PersonEnumerator(List<string> _cars)  
            {  
                cars = _cars;  
            }  
      
            //这个字段中存储的就是Person对象中的listCar集合  
            private List<string> cars;  
      
      
            //假设一开始遍历的对象的索引是-1  
            private int index = -1;  
     
            #region IEnumerator 成员  
      
      
            //表示获取当前正在遍历的那个对象  
            public object Current  
            {  
                get  
                {  
                    if (index < 0)  
                    {  
                        return null;  
                    }  
                    return cars[index];  
                }  
            }  
            //让自定义下标index累加  
            public bool MoveNext()  
            {  
                index = index + 1;  
                if (index >= cars.Count)  
                {  
                    return false;  
                }  
                else  
                {  
                    return true;  
                }  
            }  
      
            public void Reset()  
            {  
                index = -1;  
            }  
     
            #endregion  
        }  
    }  

 如果大家想要详细了解 foreach语句。建议大家学习一下迭代器。

参考网址

http://www.cnblogs.com/yangecnu/archive/2012/03/17/2402432.html

posted @ 2016-07-04 11:22  文洁丫头  阅读(1027)  评论(0编辑  收藏  举报