C#中foreach循环原理

发表于2018-10-15
评论0 3.8k浏览
在探讨foreach如何内部如何实现这个问题之前,我们需要理解两个C#里边的接口,IEnumerable 与 IEnumerator. 在C#里边的遍历集合时用到的相关类中,IEnumerable是最基本的接口。这是一个可以进行泛型化的接口,比如说IEnumerable<User>.在微软的。NET推出了这两个接口后,才有了foreach的用法,可以说,foreach是建立在这两个接口的基础之上的,foreach的前提是其里边的容器要实现了IEnumerable接口。

IEnumerable 这个接口里边定义的内容非常简单,最重要的就是里边有一个抽象方法GetEnumerator. IEnumerable的意思是这个集合是可以遍历的,而这个GetEnumerator方法返回的IEnumerator的就是一个遍历器,用这个工具来遍历这个类。如果说IEnumerable 是一瓶香槟,那么IEnumerator就是一个开瓶器。在实现这个IEnumerable接口的时候,必须要实现这个GetEnumerator方法,要返回一个实例化的IEnumorator.

下面来介绍一下这个IEnumorator接口。这个接口中定义的内容也很简单,包括Current,就是返回这个遍历工具所指向的那个容器的当前的元素,MoveNext 方法就是指向下一个元素,当遍历到最后没有元素时,返回一个false.当我们实现一个IEnumerable类的时候,我们的目的就应该是遍历这个集合,所以同时我们要实现IEnumerator这个工具类,定义我们自己的逻辑来告诉CLR我们怎么去遍历这个集合。

下面是一个简单的例子,来说明一下这两个接口的用法。
// Person类包括两个属性。  
public class Person  
{  
    public Person(string fName, string lName)  
    {  
        this.firstName = fName;  
        this.lastName = lName;  
    }  
    public string firstName;  
    public string lastName;  
}  
//People类就是Person的集合,里边使用了一个数组把单个的对象存在这个数组当中。而且因为实现了  
//IEnumerable接口,所以要实现GetEnumerator方法,返回一个实现了IEnumerator的类。  
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];  
        }  
    }  
    public PeopleEnum GetEnumerator()  
    {  
        return new PeopleEnum(_people);  
    }  
}  
// 这里我们需要定义一套逻辑去遍历上边的集合。  
public class PeopleEnum : IEnumerator  
{  
    public Person[] _people;  
    int position = -1;  
    public PeopleEnum(Person[] list)  
    {  
        _people = list;  
    }  
    public bool MoveNext()  
    {  
        position++;  
        return (position < _people.Length);  
    }  
    public void Reset()  
    {  
        position = -1;  
    }  
    object IEnumerator.Current  
    {  
        get  
        {  
            return Current;  
        }  
    }  
    public Person Current  
    {  
        get  
        {  
            try  
            {  
                return _people[position];  
            }  
            catch (IndexOutOfRangeException)  
            {  
                throw new InvalidOperationException();  
            }  
        }  
    }  
}  
class App  
{  
    static void Main()  
    {  
        Person[] peopleArray = new Person[3]  
        {  
            new Person("John", "Smith"),  
            new Person("Jim", "Johnson"),  
            new Person("Sue", "Rabon"),  
        };  
        People peopleList = new People(peopleArray);  
        //在这里使用foreach就可以遍历这个集合了,因为它实现了IEnumerable接口。  
        foreach (Person p in peopleList)  
            Console.WriteLine(p.firstName + " " + p.lastName);  
     }  
}  
/* This code produces output similar to the following: 
 * 
 * John Smith 
 * Jim Johnson 
 * Sue Rabon 
 * 
 */  

如果说,foreach后台的逻辑是这么实现的?大概是这个样子的。上边的代码会被CLR翻译成这样。
foreach (Person p in peopleList)  
            Console.WriteLine(p.firstName + " " + p.lastName);  
//翻译成  
IEnumerator enumerator = (peopleList).GetEnumerator();  
try {  
      while (enumerator.MoveNext()) {  
      Person element; //post C# 5  
      element = (Person )enumerator.Current;  
    //下边这句就是原来foreach方法体中的逻辑  
      Console.WriteLine(p.firstName + " " + p.lastName);  
   }  
}  
finally {  
   IDisposable disposable = enumerator as System.IDisposable;  
   if (disposable != null) disposable.Dispose();  
}  
附加:关于IEnumerable与ORM框架联合使用时候的延迟加载问题,以及Resharper对于此接口mutiple enumeration警告问题

使用IEnumerable的时候,Resharper经常会提示这个问题?这个问题意思是,这个集合对象可能会返回不同的遍历结果。

因为IEnumerable另外一个功能就是存放SQL一类的查询逻辑,注意,这里指的是查询逻辑,而不是真正的查询结果,也就是延迟加载。以下边的例子为例,可能objects中存放的是SQL查询逻辑。当第一次调用Any()方法的时候,会调用SQL语句查询到数据库中的结果,这时候是有一条数据的。但是objects调用First()方法来获取这个记录的时候,可能这时候的数据库已经被其他的程序改了,没有数据了,这时候就出现了dirty data的问题。所以,为了数据的一致性,需要在使用IEnumeralbe的时候,调用。ToList()方法把这些内容存放在一个个实实在在的容器中,这样就前后一致了。

还有就是从代码性能角度考虑,每次都调用一下数据库会很慢,所以干脆一下全部把数据库中符合条件的结果放到内存List中,用的时候直接从内存中拿就快多了。
public List<object> Foo(IEnumerable<object> objects)  
{  
    if(objects == null || !objects.Any())  
        throw new ArgumentException();  
var firstObject = objects.First();  
    var list= DoSomeThing(firstObject);          
    var secondList = DoSomeThingElse(objects);  
    list.AddRange(secondList);  
return list;  
} 
IEnumerable在一些ORM框架中实现了延迟加载的功能。比如说,在框架自己定义的容器对象中,实现了特定的IEnumerator接口,在MoveNext中指定逻辑,连接数据库,获取对象等。
注:C#中Dictionary字典类介绍:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionary.html
来自:https://blog.csdn.net/dk_0520/article/details/68946830

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引