为什么会出现在C#中没有ReverseEnumerator? [英] Why is there no ReverseEnumerator in C#?

查看:245
本文介绍了为什么会出现在C#中没有ReverseEnumerator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否有一个具体的理由或设计决定,不包括在C#中的反向枚举?它会这么好,如果有一个等同于C ++ reverse_iterator的就像枚举是C ++ 迭代器相当于。可以反向迭代集合只想实现类似IReverseEnumerable人们可以这样做:

 列表< INT> .ReverseEnumerator ritr = collection.GetReverseEnumerator(); 
,而(rtir.MoveNext())
{
//做的东西
}

这样,您将能够重复同样的方式列表和LinkedLists而不是使用索引器之一,并为其他从而实现更好的抽象


以前的链接< DIV CLASS =h2_lin>解决方案

这将是完全有可能实现这一点。就个人而言,我几乎从来没有反向迭代。如果我需要做到这一点,我()首先调用.Reverse。也许这是个什么.NET基础类库设计者想象的那样好。




所有功能默认情况下没有实现。他们需要设计,实现,测试,文档和支持。 - 雷蒙德陈




这就是为什么你不实施提供小工具功能。你开始与最重要的功能(如迭代从前到后)。你停止的地方在那里无论您的预算耗尽或者你觉得没有意义继续。



有很多事情是不是在.NET基类图书馆。直到.NET 4中甚至有没有一个 File.EnumerateLines 。而且我敢说,这样的功能比大多数人逆向迭代更加重要。



这可能是你在一个业务领域,其中反向运行的情况下迭代是常见的。我的经验是相反的。作为一个框架设计者,你只能猜测谁将会使用你的框架和特点是什么,这些人就会要求。这是很难划清界线。


Does anyone know if there was a specific reason or design decision to not include a reverse enumerator in C#? It would be so nice if there was an equivalent to the C++ reverse_iterator just like Enumerator is the equivalent of the C++ iterator. Collections that can be reverse-iterated would just implement something like IReverseEnumerable and one could do something like:

List<int>.ReverseEnumerator ritr = collection.GetReverseEnumerator();
while(rtir.MoveNext())
{
 // do stuff
}

This way, you would be able to iterate Lists and LinkedLists in the same way rather than using indexer for one and previous links for the other thus achieving better abstraction

解决方案

It would be entirely possible to implement this. Personally, I almost never reverse-iterate. If I need to do this, I call .Reverse() first. Probably this is what the .NET BCL designers thought as well.

All features are unimplemented by default. They need to be designed, implemented, tested, documented and supported. - Raymond Chen

And this is why you don't implement features that provide little utility. You start with the most important features (like iterating front-to-back). And you stop somewhere where either your budget is depleted or where you think is does not make sense to continue.

There are many things that are not in the .NET base class library. Until .NET 4 there even wasn't a File.EnumerateLines. And I would venture to say that such a functionality is more important than reverse iteration for most people.

It might be the case that you are working in a business domain where reverse iteration is common. My experience is the opposite. As a framework designer you can only guess who will use your framework and what features these people will demand. It is hard to draw the line.

这篇关于为什么会出现在C#中没有ReverseEnumerator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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