Iterators
An iterator is a container that puts the data that is going to be traversed, returning the same type of value through a unified interface
The iterator code returns each element in turn using the yield return statement. Yield break terminates the iteration
Implements multiple iterators in a class. Each iterator must have a unique name like any class member
The return type of the iterator must be
Ienumerable (Shaping interface), IEnumerator, ienumerable<t>, or ienumerator<t> (generic interface)
//create an iterator for an integer list Public classsamplecollection{ Public int[] items =New int[5] {5,4,7,9,3 }; PublicSystem.Collections.IEnumerable buildcollection () { for(inti =0; I < items. Length; i++) { yield returnItems[i]; } } }classProgram {Static voidMain (string[] args) {samplecollection col=Newsamplecollection (); foreach(intIinchCol. Buildcollection ())//Output Collection Data{System.Console.Write (i+" "); } for (;;) ; } }
Question: What is the difference between IEnumerator and IEnumerable?
How is the ienumerator<t> generic interface implemented?
Type comparison
Case closure and unpacking: The case is to convert the value type to System.Object, or to the interface type by value type. Unpacking the opposite.
Boxing and unpacking are to convert values to objects
structMyStruct { Public intVal; } classProgram {Static voidMain (string[] args) {MyStruct ValType1=NewMyStruct (); Valtype1.val=1; ObjectRefType =ValType1; //sealing operation, can be used for transmissionMyStruct valType2 =(mystruct) RefType; //Access value type must be unboxingConsole.WriteLine (Valtype2.val);//Output 1 for (;;) ; }
Is operator syntax:
<operand>is<type> same type returns TRUE, different types return false
As operator syntax:
<operand>is<type> converting a type to a specified reference type
Operator overloading
Public classADD2 { Public intval {Get;Set; } Public StaticAdd2operator++(Add2 op1) {op1.val= -;//Setting PropertiesOp1.val = Op1.val +2; returnOP1; } }classProgram {Static voidMain (string[] args) {ADD2 Add=NewAdd2 (); Add++; Console.WriteLine (add.val);//Output 102 for (;;) ; } }
C # Iteration Overloading, etc.