ienumerator

What is the difference between IEnumerator and IEnumerable? [duplicate]

喜欢而已 提交于 2019-12-17 05:35:08
问题 This question already has answers here : Closed 10 years ago . Possible Duplicate: Can anyone explain IEnumerable and IEnumerator to me? What are the differences between IEnumerator and IEnumerable? 回答1: IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement. Definition IEnumerable public IEnumerator GetEnumerator

How to performantly get every (unordered) pair of different collection elements if random access is not available

谁说胖子不能爱 提交于 2019-12-13 16:41:01
问题 Example: I have the collection {1, 2, 3, 4} . I want to get all (unordered) pairs of different elements, which are: {1,2} , {1,3} , {1,4} , {2,3} , {2,4} , {3,4} . If I have an IList , I can do it like this: IList<MyType> list = ... // fill the list elements for (int i = 0; i < list.Count - 1; i++) for (int j = i+1; j < list.Count; j++) { ... // now we have the pair of list[i] and list[j] } For a LinkedList I also know how to do it; it is almost the same except that instead of indexes i and j

When should I separately implement IEnumerator<T>?

风格不统一 提交于 2019-12-12 12:06:07
问题 In the framework classes of collections I have often seen IEnumerator<T> separately implemented as an inner class and an instance of it is returned in the GetEnumerator method. Now suppose I'm writing my own collection classes which will have an inbuilt collection like List<T> or T[] act as the holder internally, say like this: public class SpecialCollection<T> : IEnumerable<T> { List<T> list; public SpecialCollection<T>() { } public IEnumerator<T> GetEnumerator() { return list.GetEnumerator(

The name IEnumerator does not exist in current context

我们两清 提交于 2019-12-12 01:38:08
问题 This is the most annoying error I've had in a while. I want to make a simple loop to move my camera to another point in Unity, using C#. I'm "using System.Collections.Generic", and IEnumerator even shows up in the suggestions when I start typing it, but as soon as I'm done, it goes red and has an error that reads "Assets/Scripts/NerworkManager.cs(190,9): error CS0246: The type or namespace name `IEnumerator' could not be found. Are you missing a using directive or an assembly reference?" in

Detecting modifications with an IEnumerable

有些话、适合烂在心里 提交于 2019-12-11 03:01:03
问题 I have a question that I am surprised hasn't already been asked in exactly this format. If I have an IEnumerable that is generated based on iterating through a source of data, (and using a yield return statement), how can I detect when there has been a modification to the source after an access via an Enumerator that was generated via a GetEnumerator call? Here is the strange part: I'm not multi-threading. I think my question has a flaw in it somewhere, because this should be simple. . . I

Why do we need IEnumerator and IEnumerable?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 07:05:42
问题 Ok so I was just working through the IEnumerator and IEnumerable . Now the MSDN says that the main objective of these things is to iterate through a customer collection. Fair enough I was however able to iterate through a custom collection without the use of either (or at least I'd like to think so) namespace ienumerator { class person { public person(string first, string second) { fname = first; lname = second; } public string fname { get; set; } public string lname { get; set; } } class

List<T>.Enumerator IEnumerator.Reset() method implementation

☆樱花仙子☆ 提交于 2019-12-06 02:05:29
问题 Despite the fact, that IEnumerator.Reset method should never be used I found strange behavior of the method implementation within List<T> . No matter how you examine the .NET Framework Source Code (tried with reference source and ILSpy) the method is implemented as following: void System.Collections.IEnumerator.Reset() { if (version != list._version) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); } index = 0; current = default(T); } However

IEnumerable<T> provides two GetEnumerator methods - what is the difference between them?

主宰稳场 提交于 2019-12-05 02:44:56
When I emplement IEnumerable<T> interface I see two GetEnumerator methods: one returning IEnumerator and other IEnumerator<T> . When would I use one or another? If you are implementing the IEnumerable<T> generic interface, you will pretty much always have to use the generic GetEnumerator method - unless you cast your object explicitly to (non-generic) IEnumerable. The reason is backwards compatability with .NET 1.0/1.1 which didn't support generics. You usually implement both. One is the newer, generic version that returns a typesafe enumerator ( IEnumerator<T> ). The other one is for

IEnumerator: Is it normal to have an empty Dispose method?

≡放荡痞女 提交于 2019-12-05 00:41:00
I'm writing an IEnumerator<T> class to iterate over a COM collection I'm wrappering . I've noticed that IEnumerator<T> extends IDisposable , so I'm required to implement the Dispose method. However, I can't think of anything I would put there, as I only have a reference to the collection (which I wouldn't want being disposed at the end of a foreach ), and an int for the index. Is it normal to leave the Dispose method empty? Yes, it is. IEnumerator<T> implements IDisposable in case you make an enumerator that does need to be disposed. Since most enumerators don't need to be disposed, the method

Why does IEumerator<T> affect the state of IEnumerable<T> even the enumerator never reached the end?

 ̄綄美尐妖づ 提交于 2019-12-04 11:05:24
问题 I am curious why the following throws an error message (text reader closed exception) on the "last" assignment: IEnumerable<string> textRows = File.ReadLines(sourceTextFileName); IEnumerator<string> textEnumerator = textRows.GetEnumerator(); string first = textRows.First(); string last = textRows.Last(); However the following executes fine: IEnumerable<string> textRows = File.ReadLines(sourceTextFileName); string first = textRows.First(); string last = textRows.Last(); IEnumerator<string>