Eliminate Unnecessary Code and Boost Performance with C# 2.0 Iterators Los Angeles CA

With the .NET Framework 2.0 release now available, you can explore the C# 2.0 iterators feature. Examine the iterators design pattern and find out what it takes to implement and then modify it manually.

Local Companies

Moyea Software
92295612365
Hot building, ring street
LA, CA
Interneer Inc.
8005586832 x85
6101 W. Centinela Ave.
Culver City, CA
Cornerstone Concepts Inc
818-247-3909
600 W Broadway
Glendale, CA
Greene Computer Corporation
(818) 956-4961
200 S. Louise Street
Glendale, CA
Corticalx Inc Software Solutions & Technology
818-500-0881
425 E Colorado St
Glendale, CA
Alphatier Systems
818-409-8920
517 Griswold St
Glendale, CA
TimeTECH - Customizable Time and Attendance / Workforce Management Solutions
905-677-7009
7420 Airport Rd 203
Mississauga, CA
Hutchinson & Bloodgood, LLP
(818) 637-5000
101 N. Brand Blvd. #1600
Glendale, CA
Telsoft Solutions
818-545-8680
100 N Brand Blvd
Glendale, CA
Abraxas Technologies Inc
818-502-9100
450 N Brand Blvd
Glendale, CA

provided by: 
Originally published at Internet.com


A design pattern is a solution that focuses on solving a particular problem or issue that commonly occurs. Design patterns are often associated with object-oriented programming, but they are not exclusive to it. Non-computing disciplines also have concepts similar to the design pattern, which is likely from where programmers borrowed the concept.

The existence of a design pattern does not imply that only one solution to the problem exists, nor does it mean it is the best solution in all cases. Patterns merely provide a best-practice approach to a particular problem as learned through the countless experiences of the programming community. There are often several variations of a pattern for a particular problem. It is up to each programmer to determine if and when to apply a particular pattern. Often, the programming environment in use can influence the choice of which pattern to apply. Not all programming environments and languages support all design patterns. What may be easy to create in one environment or language may be extremely difficult in another. (Find more examples of design patterns in the article titled "Implement Common Creational Design Patterns".)

The idea behind the iterator design pattern is to expose a controlled manner to sequentially access the elements of an aggregate object without exposing its internal representation or design. Actions such as First(), Next(), CurrentItem(), and IsDone() are commonly exposed operations. Many of the System.Collection namespace classes implement the iterator pattern. Although they may use different names for the operations, they're basically the same. They expose a GetEnumerator() method, which returns the concrete iterator for the particular class. The iterator is used to traverse the structure.

Iterator Implementation Sample Code

The following sample code demonstrates an implementation of the iterator design pattern in the 1.0 and 1.1 versions of the Microsoft .NET Framework. It implements the IEnumerable interface in an outer class, which mandates the GetEnumerator method be implemented. This allows you to use it in foreach loops: using System; using System.Collections; namespace CodeGuru.IteratorsSample { class IteratorTest : IEnumerable { private ArrayList items = new ArrayList(); public int Count { get { return items.Count; } } public object this[int index] { get { return items[index]; } set { items.Insert(index, value); } } public IEnumerator GetEnumerator() { return new IteratorSampleEnumerator(this); } private class IteratorSampleEnumerator : IEnumerator { IteratorTest aggregate = null; int index = -1; public IteratorSampleEnumerator(IteratorTest aggregate) { this.aggregate = aggregate; } public virtual object Current { get { return this.aggregate[index]; } } public virtual bool MoveNext() { index++; if (index >= this.aggregate.Count) { return false; } else { return true; } } public virtual void Reset() { index = -1; } } } class Program { static void Main(string[] args) { // Create an iterator and add sample data IteratorTest tests = new IteratorTest(); tests[0] = "Test 1"; tests[1] = "Test 2"; tests[2] = "Test 3"; tests[3] = "Test 4"; tests[4] = "Test 5"; foreach (object test in tests) { Console.WriteLine(test); } // Wait for the user so we can see output Console.ReadLine(); } } }

The sample output from the application will look something like this: Test 1 Test 2 Test 3 Test 4 Test 5

To return an IEnumerator instance, you need another class that has intimate knowledge of your class and can iterate through the internal aggregate implementation.

C# 2.0 Iterators

As you may have noticed in the example, the iterator design pattern isn't overly difficult to implement. However, those of you who have read many of my prior articles know that I'm all about eliminating unnecessary code and performance overhead. Hence, the prior implementation has both unnecessary code and performance overhead. If the implementation involves value types, boxing and unboxing get involved, which leads to the performance overhead. The unnecessary code results from the iterator needing in-depth knowledge of the aggregate class. With C# 2.0, you can rely on generics and the compiler to help reduce the amount of code and performance impact. A new yield statement appears only inside an iterator block. Each call to GetEnumerator will yield the next value in the collection, and all of the state management is handled.

C# 2.0 Iterator Implementation Sample Code

The following sample code removes the nested class from the previous example. It replaced the entire GetEnumerator method to use the yield statement. Additionally, it includes the use of generics so you can apply the example to any type: using System; using System.Collections; using System.Collections.Generic; namespace CodeGuru.IteratorsSample { class IteratorTest2 : IEnumerable { private ArrayList items = new ArrayList(); public int Count { get { return items.Count; } } public T this[int index] { get { return (T)items[index]; } set { items.Insert(index, value); } } public IEnumerator GetEnumerator() { foreach (T item in this.items) { yield return item; } } } class Program { static void Main(string[] args) { // Create an iterator and add sample data IteratorTest2 tests = new IteratorTest2(); tests[0] = "Test 1"; tests[1] = "Test 2"; tests[2] = "Test 3"; tests[3] = "Test 4"; tests[4] = "Test 5"; foreach (string test in tests) { Console.WriteLine(test); } // Wait for the user so we can see output Console.ReadLine(); } } }

The sample output from the application will look just like it did before: Test 1 Test 2 Test 3 Test 4 Test 5

There is less code involved in the overall solution and the performance is improved.

Future Columns

The topic of the next column has yet to be determined. If you have something in particular that you would like to see explained, please e-mail me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer (MCSD, MCSE, MCDBA) is a senior architect of .NET applications for large and mid-sized organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the second year in a row. You can reach Mark at mstrawmyer@crowechizek.com.

Author: Mark Strawmyer

Read article at Internet.com site

Featured Local Company

Moyea Software

92295612365
Hot building, ring street
LA, CA

Related Local Events
Automation Technology Expo West (ATX West)
Dates: 2/9/2010 - 2/11/2010
Location: Anaheim Convention Center
Anaheim, CA
View Details

SOLAR POWER - Exhibition and Conference
Dates: 10/12/2010 - 10/14/2010
Location: Los Angeles Convention & Exhibition Center
Los Angeles, CA
View Details

REAL-TIME & EMBEDDED COMPUTING CONFERENCE - LONG BEACH 2009
Dates: 10/1/2009 - 10/1/2009
Location: Marriott Long Beach
Long Beach, CA
View Details

2009 IEEE Petroleum and Chemical Industry Technical Conference (PCIC 2009)
Dates: 9/14/2009 - 9/16/2009
Location:
Anaheim, CA
View Details

Medical Design & Manufacturing - Trade
Dates: 6/9/2009 - 6/11/2009
Location: CANON COMMUNICATIONS LLC
Los Angeles, CA
View Details