在Visual C# 2.0中创建优雅代码1

  热衷于C#语言的人会喜欢上Visual C# 2005。Visual Studio 2005为Visual C# 2005带来了大量令人兴奋的新功能,例如泛型、迭代器、局部类和匿名方法等。虽然泛型是人们最常谈到的也是预期的功能,尤其是在熟悉模板的C++开发人员中间,但是其他的新功能同样是对Microsoft .NET开发宝库的重要补充。与C#的第一个版本相比,这些功能和语言附加将会提高整体的生产效率,从而使开发人员能够以更快的速度写出更加简洁的代码。

 迭代器

  在C# 1.1中,可以使用foreach循环来遍历诸如数组、集合这样的数据结构:

string[] cities = {"New York","Paris","London"};

foreach(string city in cities)

{

Console.WriteLine(city);

}

  实际上,可以在foreach循环中使用任何自定义数据集合,只要该集合类型实现了返回IEnumerator接口的GetEnumerator方法即可。通常,需要通过实现IEnumerable接口来完成这些工作:

public interface IEnumerable

{

IEnumerator GetEnumerator();

}

public interface IEnumerator

{

object Current{get;}

bool MoveNext();

void Reset();

}

  在通常情况下,实现IEnumerable接口的类是作为要遍历的集合类型的嵌套类来提供的。这样,此种迭代器设计模式维持了迭代的状态。将嵌套类作为枚举器的好处是因为它可以访问其包含类的所有私有成员,而且,对迭代客户端隐藏了底层数据结构的实际实现细节,使得能够在多种数据结构上使用相同的客户端迭代逻辑,如图1所示。

图1 迭代器设计模式

  此外,由于每个迭代器都保持单独的迭代状态,所以多个客户端可以执行单独的并发迭代。通过实现IEnumerable接口,诸如数组和队列这样的数据结构可以支持这种非常规的迭代。在foreach循环中生成的代码调用类的GetEnumerator方法可以简单地获得一个IEnumerator对象,然后将其用于while循环,接着,通过连续调用它的MoveNext方法来遍历集合。如果您需要显式地遍历集合,您可以直接使用IEnumerator(无须使用foreach语句)。

  但是使用这种方法有一些问题。首先,如果集合包含值类型,则需要对它们进行装箱和拆箱才能获得项,因为IEnumerator.Current返回一个Object类的对象。这将导致潜在的性能降低 和托管堆上的压力增大。即使集合包含引用类型,仍然会产生从对象向下强制类型转换的不利结果。虽然大多数开发人员不熟悉这一特性,事实上在C# 1.0中,不必实现IEnumerator或IEnumerable接口就可以为每个循环实现迭代器模式。编译器将选择调用强类型化版本,以避免强制类型转换和装箱。结果是,即使在1.0版本中,也可能没有导致性能损失。

  为了更好地阐明这个解决方案并使其易于实现,Microsoft .NET框架2.0在System.Collections.Generics命名空间中定义了类型安全的泛型IEnumerable<ItemType>和IEnumerator<ItemType>:

public interface IEnumerable

{

IEnumerator GetEnumerator();

}

public interface IEnumerator : IDisposable

{

ItemType Current{get;}

bool MoveNext();

}

  除了利用泛型之外,新的接口与其前身还略有差别。与IEnumerable不同,IEnumerator是从IDisposable派生而来的,并且没有Reset方法。图2中的代码显示了实现IEnumerable<string>的简单city集合,而图3显示了编译器展开foreach循环的代码中如何使用该接口。图2中的实现使用了名为MyEnumerator的嵌套类,它将一个引用作为构造参数返回给要枚举的集合。MyEnumerator清楚地知道city集合(本例中的一个数组)的实现细节。此外,MyEnumerator类使用m_Current成员变量维持当前的迭代状态,此成员变量用作数组的索引。

public class CityCollection : IEnumerable<string>

{

string[] m_Cities = {"New York","Paris","London"};

public IEnumerator<string> GetEnumerator()

{

return new MyEnumerator(this);

}

//Nested class definition

class MyEnumerator : IEnumerator<string>

{

CityCollection m_Collection;

int m_Current;

public MyEnumerator(CityCollection collection)

{

m_Collection = collection;

m_Current = -1;

}

public bool MoveNext()

{

m_Current++;

if(m_Current < m_Collection.m_Cities.Length)

return true;

else

return false;

}

public string Current

{

get

{

if(m_Current == -1)

throw new InvalidOperationException();

return m_Collection.m_Cities[m_Current];

}

}

public void Dispose(){}

}

}

图2 实现IEnumerable<string>

CityCollection cities = new CityCollection();

//For this foreach loop:

foreach(string city in cities)

{

Trace.WriteLine(city);

}

//The compiler generates this equivalent code:

IEnumerable<string> enumerable = cities;

IEnumerator<string> enumerator = enumerable.GetEnumerator();

using(enumerator)

{

while(enumerator.MoveNext())

{

Trace.WriteLine(enumerator.Current);

}

}

图3 简单的迭代程序