ienumerator

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

落花浮王杯 提交于 2019-12-22 04:29:09
问题 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? 回答1: 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. 回答2: You usually implement both. One

Using ItemsSource to populate WPF ListBox - Good Idea?

余生颓废 提交于 2019-12-21 05:01:17
问题 I'm a (relatively) experienced Cocoa/Objective-C coder, and am teaching myself C# and the WPF framework. In Cocoa, when populating an NSTableView , it's relatively simply to assign a delegate and datasource to the view. Those delegate/datasource methods are then used to populate the table, and to determine its behavior. I'm putting together a simple application that has a list of objects, lets call them Dog objects, that each have a public string name . This is the return value of Dog

What is the difference between “yield return 0” and “yield return null” in Coroutine?

纵饮孤独 提交于 2019-12-21 03:55:09
问题 I'm new and a bit confused about " yield ". But finally I understand how it worked using WaitForSeconds but I can't see the difference between of " yield return 0 " and " yield return null ". are both them waiting for the next frame to execute? sorry for my bad English. Thank you very much. 回答1: Both yield return 0 and yield return null yields for a single frame. The biggest difference is that yield return 0 allocates memory because of boxing and unboxing of the 0 that happens under the hood,

Multiple Enumerators for a Single C# Class

一笑奈何 提交于 2019-12-19 03:27:24
问题 I have created a data structure consisting of intervals. The data structure should naturally have an enumerator that enumerates all intervals, but I would like to expose two different enumerators that enumerate the intervals in different order. One of the enumerators enumerate the intervals really fast, but in somewhat arbitrary order. The other enumerates them in lexicographical order, but a bit slower (depends on the intervals though). Depending on what you try to achieve, one enumerator

C# Class is IEnumerable AND an IEnumerator at the same time. What are the issues with this?

牧云@^-^@ 提交于 2019-12-19 02:07:08
问题 I have a class called GenericPermutations that is both enumerable and an enumerator. Its job is to take an ordered list of objects and iterate through each permutation of them in order. Example, an integer implemenation of this class could iterate through the following: GenericPermutations<int> p = new GenericPermutations<int>({ 1, 2, 3 }); p.nextPermutation(); // 123 p.nextPermutation(); // 132 p.nextPermutation(); // 213 // etc. So its enumerable in the sense that it contains a 'list' of

Question regarding IEnumerable and IEnumerator

冷暖自知 提交于 2019-12-18 12:32:53
问题 I use the following code to enable myClass to use foreach. But I am rather new to programming and have some difficulty in understanding the following code. I described my problems in the comments. I would be grateful for providing some information. public class MyClass : IEnumerable<string> { //1) What is IEnumerator for? // Whats the difference between IEnumerator and IEnumerable public IEnumerator<string> GetEnumerator() { yield return "first"; yield return "second"; } //2) What is it for?

How Can I Use IEnumerator.Reset()?

感情迁移 提交于 2019-12-17 15:54:13
问题 How exactly is the right way to call IEnumerator.Reset? The documentation says: The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException . Okay, so does that mean I'm not supposed to ever call it? It's so tempting to use exceptions for flow control: using (enumerator = GetSomeExpensiveEnumerator()) { while (enumerator.MoveNext()) { ... } try { enumerator.Reset(); } //Try an

How Can I Use IEnumerator.Reset()?

£可爱£侵袭症+ 提交于 2019-12-17 15:53:54
问题 How exactly is the right way to call IEnumerator.Reset? The documentation says: The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException . Okay, so does that mean I'm not supposed to ever call it? It's so tempting to use exceptions for flow control: using (enumerator = GetSomeExpensiveEnumerator()) { while (enumerator.MoveNext()) { ... } try { enumerator.Reset(); } //Try an

obtain generic enumerator from an array

北慕城南 提交于 2019-12-17 15:43:07
问题 In C#, how does one obtain a generic enumerator from a given array? In the code below, MyArray is an array of MyType objects. I'd like to obtain MyIEnumerator in the fashion shown, but it seems that I obtain an empty enumerator (although I've confirmed that MyArray.Length > 0 ). MyType [ ] MyArray = ... ; IEnumerator<MyType> MyIEnumerator = ( MyArray.GetEnumerator() as IEnumerator<MyType> ) ; 回答1: Works on 2.0+: ((IEnumerable<MyType>)myArray).GetEnumerator() Works on 3.5+ (fancy LINQy, a bit

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

孤街醉人 提交于 2019-12-17 05:35:26
问题 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