terça-feira, 10 de março de 2009

Iterators at C# Online.NET (CSharp-Online.NET)

Iterators
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio

Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

* Manage data
* Manage collections
o ArrayList class
o Collection interfaces
o Iterators
o Hashtable class
o CollectionBase
o DictionaryBase
o Comparer class
o Queue class
o SortedList class
o BitArray class
o Stack class
* Generic collections
* Specialized collections
* Implement interfaces
* Events and delegates

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text
edit

Iterators are an alternative to implementing IEnumerable. They are new in C# 2.0 and greatly reduce the complexity of providing an enumerator for a collection.

When using an iterator, the compiler generates the IEnumerator class at build time.

The keyword yield is used with a return statement to return a single item at a time. Each yield return corresponds to getting the current item. Then when the next item is requested, the compiler resumes the execution after the last yield return was called. In reality, the execution of the method does not begin at the beginning of the method each time. The compiler turns the single yield method into an entire IEnumerator class with its own state, MoveNext and Current.

However, when implementing, do not focus on how the compiler accomplishes its task. Instead, simply yield one item at a time in the order desired using whatever technique is easiest.
Contents
[hide]

* 1 Code examples
* 2 Generic Usage
o 2.1 Iterating with foreach
o 2.2 Non-Generic
o 2.3 Generic
* 3 MSDN References

[edit]
Code examples

Iterate over some constant values

public IEnumerable Values(){

yield return 2;
yield return 55;
yield return 34;
yield return 324;
yield return 1;
yield return 7;
yield return 98;
yield return 34;
yield return 4;
yield return 11;

}

Iterate through an internal array

private int[] items;

//...

public IEnumerable Items(){

for (int i = 0; i < items.Length; i++){
yield return items[i];
}

}

Iterate through n prime numbers

public IEnumerable Primes( int max ){

for (int i = 0; i < max; i++){
if( IsPrime( i ) ){
yield return i;
}
}

}

Iterate forever

// This has no end
// It will always return Hello after every MoveNext
// However, it is not an infinite loop
// The one who uses the enumerator determines
// whether to keep calling MoveNext
public IEnumerable EternalEcho(){

while( true ){
yield return "Hello";
}

}


[edit]
Generic Usage

To promote type safety it is recommended that you use the generic IEnumerable form via IEnumerable. As with all other generic classes, when you use them you are moving a possible error from run-time to compile/design-time.


[edit]
Iterating with foreach

static void Main(string[] args)
{
foreach (TypeCode currentType in getCode())
{
//Do Stuff
}
}

[edit]
Non-Generic

You may want to do this at some point, it will compile, but can the caller handle it. If using a foreach loop then a cast occurs for each item. Depending on how you use the object inside the foreach loop you may have exceptions being thrown as 55000 is not an enumerated TypeCode.

static IEnumerable getCode()
{
yield return TypeCode.Boolean;
yield return TypeCode.Byte;
yield return 55000;
yield return TypeCode.Char;
}

[edit]
Generic

The compiler sees that this could cause problems and does not compile it.

static IEnumerable getCode()
{
yield return TypeCode.Boolean;
yield return TypeCode.Byte;
yield return 55000;
yield return TypeCode.Char;
}

If you are 100% sure that it is what you want to do you could cast it. But only if you must!

static IEnumerable getCode()
{
yield return TypeCode.Boolean;
yield return TypeCode.Byte;
//You can persuade the compiler but you may get yourself into problems!
yield return (TypeCode) 55000;
yield return TypeCode.Char;
}

[edit]
MSDN References

Iterators
Retrieved from "http://en.csharp-online.net/Iterators"

Categories: Exam 70-536 Study Guide | Exam 70-536 Study Guide: Skill Set 1

Nenhum comentário: