C#
  Home arrow C# arrow Page 2 - Patterns and Iterators in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Patterns and Iterators in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-10-16

    Table of Contents:
  • Patterns and Iterators in C#
  • Enumeration and Iterators
  • Iterators
  • Iterator Semantics

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Patterns and Iterators in C# - Enumeration and Iterators


    (Page 2 of 4 )

    Enumeration

    An enumerator is a read-only, forward-only cursor over a sequence of values. An enumerator is an object that either:

    • ImplementsIEnumeratororIEnumerator<T>
       
    • Has a method namedMoveNext for iterating the sequence, and a property calledCurrentfor getting the current element in the sequence

    Theforeachstatement iterates over an enumerable object. An enumerable object is the logical representation of a sequence. It is not itself a cursor, but an object that produces cursors over itself. An enumerable object either:

    • ImplementsIEnumerableorIEnumerable<T> 
    • Has a method namedGetEnumeratorthat returns an enumerator

    IEnumeratorandIEnumerableare defined inSystem.Collections.

    IEnumerator<T>andIEnumerable<T>are defined inSystem.Collections.Generic.

    The enumeration pattern is as follows:

      class Enumerator   // typically implements IEnumerator or IEnumerator<T>
      {
       
    public IteratorVariableType Current { get {...} }
       
    public bool MoveNext()         {...}
      }

      class Enumerable   // typically implements IEnumerable or IEnumerable<T>
      {
       
    public Enumerator GetEnumerator() {...}
      }

    Here is the high-level way of iterating through the characters in the word “beer” using aforeachstatement:

      foreach (char c in "beer")
        Console.WriteLine (c);

    Here is the low-level way of iterating through the characters in “beer” without using aforeachstatement:

      var enumerator = "beer".GetEnumerator();

      while (enumerator.MoveNext())
      {
       
    var element = enumerator.Current;
       
    Console.WriteLine (element);
     
    }

    Theforeachstatement also acts as ausing statement, implicitly disposing the enumerator object.

    Chapter 7 explains the enumeration interfaces in further detail.

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek