1. C# iterator
1.1. IEnumerable and IEnumerator
Iterators in C# are encapsulated in IEnumerable and IEnumerator and their generic interfaces.
IEnumerable: defines a method that can obtain IEnumerator ---GetEnumerator().
//IEnumerable code implementation public interface IEnumerable { IEnumerator GetEnumerator(); }
IEnumerator: then implements loop iteration on the target sequence until data is no longer needed. A common foreach loop is to use this interface
//IEnumerator code implementation public interface IEnumerator { /// <summary> /// Get the current item (read-only attribute) /// </summary> object Current { get; } /// <summary> /// Move the cursor's internal position forward /// </summary> /// <returns> Whether it was successful</returns> bool MoveNext(); /// <summary> /// Reset the cursor in front of the first member /// </summary> void Reset(); }
But why are there two interfaces? Why not just let IEnumerable implement all methods?
In order not to violate the principle of single responsibility. If there are several nested foreach loops, then each other may be affected.
1.2. Iterator usage
Arrays and collections in C# both inherit the IEnumerable interface, so we can use them directly.
static void Main(string[] args) { int[] arr = new int[5] { 12, 65, 749, 16, 49 }; //foreach (int item in arr) //{ // (item); //} #region is the same as the foreach above IEnumerator e = (); //Get the object to iterate while (()) //Read elements one by one, and after reading, it will return false { (); //Output read content } #endregion }
Use iterators in custom classes.
/// <summary> /// Zoo, there are many animals in it /// </summary> public class Zoo : IEnumerable { Animal[] animals = new Animal[3]; public Zoo() { animals[0] = new Animal("Zhang San"); animals[1] = new Animal("Li Si"); animals[2] = new Animal("Zhao Wu"); } public IEnumerator GetEnumerator() { return (); } } /// <summary> ///Animals, each animal has its own name /// </summary> public class Animal { private string name; public string Name { get { return name; } } public Animal(string name) { = name; } } static void Main(string[] args) { Zoo zoo = new Zoo(); //You can use foreach iteration directly foreach (Animal animal in zoo) { (); } (); }
2. Coroutines in Unity
2.1. Process Thread Coroutine Introduction
process:
- Processes have their own independent heap and stack, neither sharing heap nor sharing stack, and processes are scheduled by the operating system.
- A process is a program with independent functions about a running activity on a certain data set.
Thread:
- Threads have their own independent stack and shared heap, and share heap, but do not share stack. Threads are scheduled by the operating system.
- A process can contain multiple threads.
- Threads run asynchronously, and multiple threads in a multiprocessor can run at the same time.
Coroutine:
- Coroutines share heap, not share stack, coroutines are scheduled by programmers in the code of coroutines.
- Coroutine execution is on the main thread of the game, and only one coroutine can be executed at the same time.
- Threads avoid meaningless scheduling and improve performance, but programmers need to assume their own scheduling responsibilities.
2.2. Coroutine usage
Unity's coroutine system is a simple and powerful interface based on C#, IEnumerator.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.