ienumerator

Why do arrays in .net only implement IEnumerable and not IEnumerable<T>?

萝らか妹 提交于 2019-11-27 19:16:36
I was implementing my own ArrayList class and was left surprised when I realised that public System.Collections.Generic.IEnumerator<T> GetEnumerator() { return _array.GetEnumerator(); } didn't work. What is the reason arrays don't implement IEnumerator in .NET? Is there any work-around? Thanks Arrays do implement IEnumerable<T> , but it is done as part of the special knowledge the CLI has for arrays. This works as if it were an explicit implementation (but isn't: it is done at runtime). Many tools will not show this implementation, this is described in the Remarks section of the Array class

Why does capturing a mutable struct variable inside a closure within a using statement change its local behavior?

≡放荡痞女 提交于 2019-11-27 11:39:46
问题 Update : Well, now I've gone and done it: I filed a bug report with Microsoft about this, as I seriously doubt that it is correct behavior. That said, I'm still not 100% sure what to believe regarding this question; so I can see that what is "correct" is open to some level of interpretation. My feeling is that either Microsoft will accept that this is a bug, or else respond that the modification of a mutable value type variable within a using statement constitutes undefined behavior. Also,

Why does IEnumerator<T> inherit from IDisposable while the non-generic IEnumerator does not?

爷,独闯天下 提交于 2019-11-27 06:47:08
I noticed that the generic IEnumerator<T> inherits from IDisposable, but the non-generic interface IEnumerator does not. Why is it designed in this way? Usually, we use foreach statement to go through a IEnumerator<T> instance. The generated code of foreach actually has try-finally block that invokes Dispose() in finally. Jon Skeet Basically it was an oversight. In C# 1.0, foreach never called Dispose 1 . With C# 1.2 (introduced in VS2003 - there's no 1.1, bizarrely) foreach began to check in the finally block whether or not the iterator implemented IDisposable - they had to do it that way,

What is the best way to convert an IEnumerator to a generic IEnumerator?

一个人想着一个人 提交于 2019-11-27 04:41:28
问题 I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and I am wanting to expose the IEnumerator as a generic IEnumerator. What would be the best way to achieve this? I am currently using the code: public new IEnumerator<GenericObject> GetEnumerator() { var list = new List(); var baseEnum = base.GetEnumerator(); while(baseEnum.MoveNext()) { var obj = baseEnum.Current as GenericObject; if (obj != null) list.Add(obj); } return list.GetEnumerator();

Unity - IEnumerator's yield return null

房东的猫 提交于 2019-11-27 03:27:24
问题 I'm currently trying to understand IEnumerator & Coroutine within the context of Unity and am not too confident on what the "yield return null" performs. At the moment i believe it basically pauses and waits for the next frame and in the next frame it'll go back to perform the while statement again. If i leave out the "yield return null" it seems the object will instantly move to its destination or perhaps "skip a lot of frames". So i guess my question is how does this "yield return null"

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

╄→гoц情女王★ 提交于 2019-11-26 21:25:13
Possible Duplicate: Can anyone explain IEnumerable and IEnumerator to me? What are the differences between IEnumerator and IEnumerable? 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(); IEnumerator public object Current; public void Reset(); public bool MoveNext(); example code from codebetter.com An IEnumerator is a thing that can enumerate: it has

Why do arrays in .net only implement IEnumerable and not IEnumerable<T>?

廉价感情. 提交于 2019-11-26 19:48:34
问题 I was implementing my own ArrayList class and was left surprised when I realised that public System.Collections.Generic.IEnumerator<T> GetEnumerator() { return _array.GetEnumerator(); } didn't work. What is the reason arrays don't implement IEnumerator in .NET? Is there any work-around? Thanks 回答1: Arrays do implement IEnumerable<T> , but it is done as part of the special knowledge the CLI has for arrays. This works as if it were an explicit implementation (but isn't: it is done at runtime).

Why do BCL Collections use struct enumerators, not classes?

懵懂的女人 提交于 2019-11-26 18:28:48
We all know mutable structs are evil in general. I'm also pretty sure that because IEnumerable<T>.GetEnumerator() returns type IEnumerator<T> , the structs are immediately boxed into a reference type, costing more than if they were simply reference types to begin with. So why, in the BCL generic collections, are all the enumerators mutable structs? Surely there had to have been a good reason. The only thing that occurs to me is that structs can be copied easily, thus preserving the enumerator state at an arbitrary point. But adding a Copy() method to the IEnumerator interface would have been

Why do BCL Collections use struct enumerators, not classes?

╄→гoц情女王★ 提交于 2019-11-26 06:20:00
问题 We all know mutable structs are evil in general. I\'m also pretty sure that because IEnumerable<T>.GetEnumerator() returns type IEnumerator<T> , the structs are immediately boxed into a reference type, costing more than if they were simply reference types to begin with. So why, in the BCL generic collections, are all the enumerators mutable structs? Surely there had to have been a good reason. The only thing that occurs to me is that structs can be copied easily, thus preserving the

Can anyone explain IEnumerable and IEnumerator to me? [closed]

做~自己de王妃 提交于 2019-11-26 00:56:28
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 months ago . Can anyone explain IEnumerable and IEnumerator to me? for example, when to use it over foreach? what\'s the difference between IEnumerable and IEnumerator? Why do we need to use it? 回答1: for example, when to use it over foreach? You don't use IEnumerable "over" foreach .