1

C# 9 新特性 —— 增强的 foreach

 3 years ago
source link: http://www.cnblogs.com/weihanli/p/14204347.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C# 9 新特性 —— 增强的 foreach

Intro

在 C# 9 中增强了 foreach 的使用,使得一切对象都有 foreach 的可能

我们来看一段代码,这里我们试图遍历一个 int 类型的值

3uAfMzA.png!mobile

思考一下,我们可以怎么做使得上面的代码编译通过呢?

迭代器模式

迭代器模式,提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。

迭代器模式是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可以让外部代码透明地访问集合内部的数据。

foreach 其实是一个迭代器模式的语法糖,用来遍历一个集合中的数据, foreach 可以使用 while 来实现,比如下面这个示例:

var enumerable = Enumerable.Range(1, 10).ToArray();
foreach (var i in enumerable)
{
    Console.WriteLine(i);
}

使用 while 重写之后类似下面这样的代码:

var enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
{
    Console.WriteLine(enumerator.Current);
}

c# 中的集合基本都实现了迭代器模式,可以直接使用 foreach 来遍历,对于自定义的类型想要支持 foreach 可以实现 IEnumerableIEnumerable<T> ,对于没有实现迭代器的代码,是不是可以用 foreach

Enumerator

我们再来看开篇提到的问题,怎么实现支持 foreach

R7ZzemU.png!mobile

从上面 VS 的提示我们可以看得出来,如果一个类型想要支持 foreach ,有三种方式可以实现:

  1. 实现 IEnumerable
  2. 实现 IEnmuerable<T>
  3. 添加 GetEnumerator 方法,方法返回值类型需要有 Current 属性和 MoveNext 方法,可以参考这个 IEnumerator ,返回类型可以直接实现 IEnumeratorIEnumerator<T>

那么如果是一个别人封装的类型,能否支持 foreach 呢,从 C# 9 之后就可以了,可以添加一个 GetEnumerator 的扩展方法,类似于下面

public static class ForEachExtensions
{
    public static IEnumerator<char> GetEnumerator(this int num)
    {
        return num.ToString().GetEnumerator();
    }
}

此时如果是使用 C# 9 就可以编译通过了,如果手动设置了 LangVersion ,需要修改为 9,否则会得到类似下面这样的错误

uyyUNfz.png!mobile

添加使用扩展方法,并启用 C# 9 语法:

Q7nm2ue.png!mobile

Aj67Njb.png!mobile

More

有了这个功能之后,一切类型都是可以 foreach 的,没有实现迭代器模式的类型,只需要实现一个扩展方法就可以了

迎接 C# 9 ,万物皆可 foreach ~~

Reference


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK