Enumerator mode (enumerator pattern)

Source: Internet
Author: User
Tags object object

How to use Enumerator mode (enumerator pattern):

1, if you want to implement Ienumerable.getenumerator, you also want to implement a GetEnumerator method version of the Non-virtual method. The Ienumerable.getenumerator method of your enumerator class should call this non-virtual method. You should return an inline enumerator structure as shown below:



· Class Myclass:ienumerable



· {



· Non-virtual implementation for your custom collection



· Public Myenumerator GetEnumerator () {



· Return to New Myenumerator (this); return nested public struct



· }



· IEnumerator implementation



· Public Ienumerator.getenumerator () {



· Return GetEnumerator ()://call the Non-interface method



· }



· }



If your class has been shown to provide a non-virtual GetEnumerator method, then the foreach statement will call this non-virtual GetEnumerator method. Otherwise, it will call the Ienumerable.getenumerator method if your class implements the IEnumerable interface. Calling a Non-virtual method is more efficient than calling a virtual method through an interface.



2, the implementation ienumerator.current attribute displayed in the enumerator structure ... NET implementation class, when implemented, causes the property to return a System.Object object instead of a strongly typed object. There is a type conversion overhead. You can avoid the overhead of type conversions by returning a strongly typed object or an exact value type instead of a System.Object in your current attribute. Because you have shown that you have implemented a Non-virtual GetEnumerator method (not a Ienumerable.getenumerator method), the CLR can invoke the Enumerator.current property directly, not the IEnumerator.Current property, because This can directly capture the expected data, as well as avoid the overhead of type conversions and boxing operations. To reduce the virtual method invocation, you can use the inline of the method. Your implementation should be as follows:



Custom property in your class



Call this property to avoid the boxing or casting overhead



Public MyValueType Current {



MyValueType obj = new MyValueType ();



The obj fields are populated here



return obj;



}



EXPLICIT member implementation



Object IEnumerator.Current {



get {return current}//Call the Non-interface property to avoid casting



}



Note: The enumerator pattern applies only if performance is critical.

The following example code shows the enumerator pattern:

public class Itemtypecollection:ienumerable



{



public struct Myenumerator:ienumerator



{



Public ItemType Current {get {...}}}



Object IEnumerator.Current {get {return current;}}



public bool MoveNext () {...}



... }



Public Myenumerator GetEnumerator () {...}



IEnumerator Ienumerable.getenumerator () {...}



...}



To use JIT inline, you should avoid using virtual methods in your collection classes unless you really need to extend them. At the same time, the current value is returned in the implementation of the present property to allow for inline use, or to use a field.


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.