Introduction


Two of the four main new features in C# 2.0 are iterators and partial types. Iterators allow for a block of code to yield an ordered sequence of values. Partial types allow for different parts of classes to be placed in different locations.

Iterators are a mechanism whereby a class can enumerate data using the foreach loop construct. However, iterators are much more flexible than this. You can easily generate a sequence of data returned by the enumerator; it does not have to be hardcoded up front. For example, you could easily write an enumerator that generates the Fibonacci sequence on demand. Another flexible feature of iterators is that you do not have to set a limit on the number of values returned by the iterator, so in this example you could choose when to stop producing the Fibonacci sequence.

Previous versions of the .NET Framework required you to perform several steps to allow the foreach construct to operate on your type. First, you had to implement the IEnumerable interface on your type, then you had to implement the IEnumerator interface on another type. This second type performed the actual work to enable foreach functionality. The methods MoveNext and Reset, along with the Current property, then had to be written by hand inside this type.

Iterators allow you to hand the work of writing this class off to the C# compiler. With Version 2.0 of the C# compiler, the ability for a type to be used by a foreach loop requires much less work. Now you need to add only an iterator to your type. An iterator is a member within your type (e.g., a method, an operator overload, or the get accessor of a property) that returns either a System.Collections.IEnumerator, a System.Collections.Generic.IEnumerator<T>, a System.Collections.IEnumerable, or a System.Collections.Generic.IEnumerable<T> and that contains at least one yield statement. This simplicity allows you to more easily write types that can be used by foreach loops.

Partial types allow the developer to split pieces of a type across several areas where the type is defined. The type can be in multiple files, multiple areas in the same file, or a combination of the two. Declaring a type as partial is an indicator to the C# compiler that this type may not be fully represented in this location and that it cannot be fully compiled until the other parts are found or the end of the list of modules to compile is found. Partial types are purely a compiler-implemented feature with no impact to the underlying Microsoft Intermediate Language that is generated for the class. The main examples of using partial types are in the Visual Studio IDE, where the designer uses them to keep designer-generated code separate from UI logic the developer creates, and in the DataSet creation code, which is based on an XML Schema Definition of the data. Even though partial types are only a compiler-level feature, you can use them to your advantage in a few situations that are pointed out in Recipes 6.10 and 6.11.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net