C # Iteration Overloading, etc.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.