قالب وردپرس درنا توس
Home / Tips and Tricks / How do counters and quantities work in C #? – CloudSavvy IT

How do counters and quantities work in C #? – CloudSavvy IT

C #’s IEnumerable and IEnumerator interfaces are used by collections such as Arrays and Lists to standardize methods to loop over them and perform actions, such as filtering and selecting with LINQ statements. We discuss how they work and how to use them.

What is a number?

The term “Enumerable” defines an object that is intended to be iterated over, passing over each element once in order. IC #, an Enumerable is an object that a matrix, a list, or some other type of collection that implements IEnumerable interface. Enumerables standardizes looping across collections and enables the use of LINQ query syntax, as well as other useful extension methods such as List.Where() or List.Select().

All IEnumerable interface requires is a method, GetEnumerator(), which returns one IEnumerator. So what do Enumerators do?

Well, IEnumerator requires two methods, MoveNext() and Current(). MoveNext is the method used to go over each object, apply all types of custom iterator logic in the process, and Current is a method used to get the current article after MoveNext is done. You end up with an interface that defines objects that can be enumerated and how to handle that enumeration.

For example, every time you write one foreach loop, you use enumerators. You actually can not use foreach on a collection that does not implement IEnumerable. When compiled, a foreach loop like this:

foreach (Int element in list)
    // your code

Turns into a time loop, which is processed until the number of objects ends MoveNext each time and set the iterator variable to .Current.

IEnumerator enumerator= list.GetEnumerator();
while (list.MoveNext())
    element = (Int)enumerator.Current
    // your code

Most of the time you want to have to manually implement some of this, because Enumerables as Lists are designed to be used in loops and with LINQ methods. But if you want to create your own collection class, you can do so by implementing IEnumerable and returning the enumerator to the list where you store data.

For classes that have fields that you want to repeat, it can be an easy way to clear the code. Instead of the data field being public and repeated CustomCollection.dataArray, you can only iterate over CustomCollection itself. Of course, if you want it to behave more like a proper list, you need to implement other interfaces like ICollection as well.

LINQ Queries uses deferred execution

The most important thing to note about LINQ is that questions are not always run directly. Many methods return quantities and use deferred execution. Instead of looping right when the query is done, it actually waits until the Enumerable is used in the loop and then does the query below MoveNext.


7;s look at an example. This function takes a list of strings and prints each object that is longer than a certain length. It uses Where() to filter the list and then foreach to print each element.

This syntax is a bit confusing, especially if you have a habit of using it var (which you definitely should not do here), as it may look like you’re generating a new list with Where() and then iterate over that list. When it comes to a pre-loop, an IEnumerable can take the place of a list, but it’s not a list – you can not add or remove items.

However, C # is smart, and instead of iterating over the list twice (which would double the runtime for no reason and allocate useless memory), it just chucks the filtering logic into foreach loop. This is not exactly what your code is translated into, but it is a good idea to conceptualize it this way with the condition and filtering being crushed in advance.

Under the hood, it does this by preparing the query for the Enumerator and returning an Enumerable that will use the logic from Where() when you call Enumerable.MoveNext(). If you are interested, you can take a look at how System.Linq.Enumerable handles it System.Core.

This can actually have some negative effects if you are not careful. Think again, between Where() and the execution changed the collection? If you do, questions may appear to be incorrect.

For example, let’s give the same function some input, but rather than printing directly, we add a new word to the list before foreach. At first glance, it may look like it will filter out the only letters and output “Hello World”, but it output “Hello World Banana”, as the filtering is not applied until MoveNext called during the actual iteration of foreach.

This is especially sneaky, because we do not even touch toPrint variable transferred to the foreach loop. But if you add instead .ToList() to the end of Where function, which will force C # to perform an iteration and return a new list. But if it is not specifically what you want, it is almost always better to save on execution time and allocation of piles using foreach with a plate.

What are coroutins?

Coroutines are special features that can pause driving and return to the main thread. They are usually used to perform long actions that may take some time to complete, without making the application hang while you wait for the routine. For example, in games where the framerate of the application is very important, large hitches even on the order of a few milliseconds would damage the user experience.

Coroutines are quite closely linked to Enumerators, as they are really just features that return a teller. This defines an iterator method, and you can use it. You do not have to do much; just change the return type of the function to IEnumerator, and rather than returning something, you can only yield return, which automatically defines it as an enumerator without explicitly doing so.

To break up the driving over different frames, you just need to do it yield return null when you want to pause the run and run more of the main thread. To use this Enumerator, you must call the function and assign it to a variable that you can call MoveNext() on at regular intervals.

If you use Unity, they have a coroutine controller that can handle the processing of coroutines for you, just by starting and stopping them with their methods. But all it really does is just call MoveNext() once per frame, check if it can be processed more and handle the return value if it is not zero. You can handle it yourself quite easily:

Of course, you may want extra functionality, such as providing choreographies for children that you need to add to a treatment bar and provide expressions such as WaitForSeconds or waiting for async functions to complete, which you only need to check once per frame (before calling MoveNext) if the expression returns true and then continues processing. In this way, the koroutin will stop completely until it can start running again.

Source link