Note that there are some explanatory texts on larger screens.

plurals
  1. PORationale of C# iterators design (comparing to C++)
    text
    copied!<p>I found similar topic:<br> <a href="https://stackoverflow.com/questions/56347/iterators-in-c-stl-vs-java-is-there-a-conceptual-difference">Iterators in C++ (stl) vs Java, is there a conceptual difference?</a></p> <p>Which basically deals with Java iterator (which is similar to C#) being unable to going backward.</p> <p>So here I would like to focus on limits -- in C++ iterator does not know its limit, you have by yourself compare the given iterator with the limit. In C# iterator knows more -- you can tell without comparing to any external reference, if the iterator is valid or not.</p> <p>I prefer C++ way, because once having iterator you can set any iterator as a limit. In other words if you would like to get only few elements instead of entire collection, you don't have to alter the iterator (in C++). For me it is more "pure" (clear).</p> <p>But of course MS knew this and C++ while designing C#. So what are the advantages of C# way? Which approach is more powerful (which leads to more elegant functions based on iterators). What do I miss?</p> <p>If you have thoughts on C# vs. C++ iterators <strong>design</strong> other than their limits (boundaries), please also answer.</p> <p><strong>Note</strong>: (just in case) please, keep the discussion strictly technical. No C++/C# flamewar.</p> <h1>EDITS</h1> <p>As Tzaman put it "There's no benefit to having the limit be expressed separately, because there's no way to get there except walking one element at a time." However it is not that difficult to built a C# iterator which does several steps a time, thus the question -- is there a benefit of having explicit limit iterator (as in C++)? If yes -- what?</p> <h2>@Jon,</h2> <p><em>Edit1:</em> let's say you have a function foo which does something on iterators (the example is very naive!)</p> <pre><code>void foo(iter_from,iter_end) // C++ style void foo(iter) // C# style </code></pre> <p>And now you would like to call function bar on all elements except last 10.</p> <pre><code>bar(iter_from,iter_end-10); // C++ style </code></pre> <p>In C# (if I am not mistaken) you would have to provide extra method for this iterator to change <em>its</em> limit, something like this:</p> <pre><code>bar(iter.ChangeTheLimit(-10)); </code></pre> <p><em>Edit2:</em> After rereading of your post, I sense crucial difference. In C++ you work on iterators of collection, in C# you work on collections (iterator is used "internally"). If yes, I still feel a bit uncomfortable with C# -- you iterate the collection and when you find interesting element you would like to pass all elements from "here" to end. In C++ it is very easy and there is no overhead. In C# you either pass an iterator or a collection (if the latter there will be extra computing). I'll wait for your comment :-)</p> <h2>@Hans,</h2> <p>I am not comparing apples and oranges. The comp. theory is common ground here, so you have sort algorithm, partition, etc. You have the notion of collection (or sequence, as Jon likes). Now -- the matter is how you design the access to the elements to have elegant algorithm written in C# or C++ (or any other language). I would like to understand the reasoning "we did this, because...".</p> <p>I know .NET iterators and collections are separate classes. I know the difference between access to an element and entire collection. However in C++ the most general way to work on a collection is working with iterators -- this way you can work with list and vector despite those collections are completely different. In C# on the other hand you rather write </p> <pre><code>sort(IEnumerable&lt;T&gt; coll) </code></pre> <p>function instead </p> <pre><code>sort(IEnumerator&lt;T&gt; iter) </code></pre> <p>correct? So in this sense I guess you cannot take C# iterators as C++ iterators, because C# does not express the same algorithms the same way as C++ does. Or as Jon pointed out -- in C# you rather transform collection (Skip, Take) instead of changing iterators.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload