Menu
  • HOME
  • TAGS

F#: Downcast seq to IEnumerator

f#,ienumerator,seq

A sequence expression creates an object that implements IEnumerable<T> and IEnumerator<T> let s = seq { for i in 0 .. 4095 do yield i } printfn "%b" (s :? IEnumerable<int>) // true printfn "%b" (s :? IEnumerator<int>) // true But Seq.init does not: let s = Seq.init 4095 (fun...

C# - Create Enumeraor that Get Values Randomly

c#,ienumerable,ienumerator

Using iterators with Random and infinite loop would be simplest solution. public IEnumerable<T> GetRandomElements<T>(T[] elements) { Random rand = new Random(); while(true) { int index = rand.Next(0, elements.Length); yield return elements[index]; } } ...

Is ok for implementations of IEnumerator.MoveNext() to include a long running process?

ienumerator

There are no guidelines about the expected performance of MoveNext(), unlike the ones that exist for property getters, for example. You approach looks reasonable. Still, it would be wise to include a note about the behavior in the class/method docs....

foreach can not get an enumerator of a struct element

c#,foreach,ienumerator

You can't iterate over something that is not a collection or otherwise iterable type. Based on your updates, here is what I believe you are looking for: foreach (sorular s in soru) { var i = s.dogrucevap; ... } ...

return type of public IEnumerator GetEnumerator()?

c#,ienumerator

How can this function return integer It doesn't, it yield returns an integer. This means that it can be compiled into a method that returns IEnumerable, IEnumerable<int>, IEnumerator or IEnumerator<int> depending on which your method says it returns. The yield keyword is a convenience that allows for the creation...

Coroutine Won't Yield

c#,unity3d,coroutine,yield-return,ienumerator

Solved it... Vector3 move_to = new Vector3(transform.position.x-1.5f, col.contacts[0].point.y+1.5f, transform.position.z-1.5f); while(Vector3.Distance(transform.position, move_to) > 0.01f) { rigidbody.velocity = Vector3.zero; rigidbody.angularVelocity = Vector3.zero; rigidbody.Sleep(); transform.LookAt(col.transform.position); transform.position = Vector3.Lerp(transform.position, move_to, 5f * Time.deltaTime); yield return null; } This while loop was causing the rigidbody to wake up and go to sleep everytime it looped....

Iterate over IEnumerable stops after first iteration

c#,ienumerable,ienumerator

Not sure why you need a List<T> there. if (idx < reeks.Count - 1) in MoveNext fails in second iteration. If it isn't necessary to implement your custom iterator, I'd do it with iterator blocks. private static IEnumerable<int> Reeks() { int num = 1; yield return num; while (true) {...

How to write GetEnumerator() for a generic class?

c#,ienumerator

Implement IEnumerable<T>, it's not mandatory for use in foreach but it's best practice so you can cast your struct to IEnumerable<T> and get extension method support: public class ReadStruct<T> : IEnumerable<T> where T : struct Then you can implement GetEnumerator using yield return and reusing your indexer: public IEnumerator<T>...

Need Help Yielding a Coroutine

c#,unity3d,coroutine,yield-return,ienumerator

this part: foreach(Material material in materialsList){ StartCoroutine(ChangeToWhite(material)); } has to contain the wait time of the change to white routine. In your case, it would be variable because you are not using Time.deltaTime which is HIGHLY encouraged by the way. EDIT: here is the code you may want to use...

c# Implementing two enumerators for the same class

c#,list,ienumerable,ienumerator

Don't expose a List<T>, expose something else, like an IReadOnlyList<T> instead: class bar { private readonly List<foo> a = new List<foo>(); private readonly List<foo> b = new List<foo>(); public IReadOnlyList<foo> A { get; private set; } public IReadOnlyList<foo> B { get; private set; } public bar() { A = a.AsReadOnly();...

implementing IEnumerable and IEnumerable.GetEnumerator() can not be public, why?

c#,.net,enumerator,ienumerator

When implemented explicitly interface methods are public by default and that's why you cannot use access modifiers. A quote from msdn.com : When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface (which is public by default) source...

How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator's MoveNext?

c#,linq,visual-studio,enumerator,ienumerator

Visual Studio uses a feature called FuncEval, which basically lets the IDE run your code. This enables a lot of nice features, but obviously there are numerous caveats of doing so. Examples are side effects on properties, deadlocks, skipping steps in a loop, and weird debugging experiences.

When should I separately implement IEnumerator? [closed]

c#,.net,enumerator,ienumerator,design-guidelines

The answer to your first question is: when a yield return doesn't meet your needs. The answer to your second question is: these heavily used types have performance requirements that are unusually strict, so the enumerators are custom built. I've written some articles on this recently; see: http://ericlippert.com/2014/05/21/enumerator-advance/ http://ericlippert.com/2014/06/04/enumerator-bounds/...

How real enumerator for collection is implemented?

c#,list,ienumerator

how is it done? Trivial. Keep a version counter. Compare version counter when the enumerator was created to the one currently in the enumerated object on every yield. Finished. Obviously: change version on every change. Now, "i didn't find anything solid" - did it ever occur to you to...

GetEnumerator does not exist in this context

c#,linked-list,ienumerable,ienumerator

IEnumerable<string>.GetEnumerator() is an explicit interface implementation, so you need to call it on the instance, accessed as an interface. The easiest way to do this is to cast this: return ((IEnumerable<string>)this).GetEnumerator(); See How to call explicit interface implementation methods internally without explicit casting? for alternatives....