/* A Programmer's Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3 */ // 19 - Indexers and EnumeratorsEnumerators and Foreach // copyright 2000 Eric Gunnerson using System; using System.Collections; // Note: This class is not thread-safe class IntList: IEnumerable { int[] values = new int[10]; int allocated = 10; int count = 0; int revision = 0; public void Add(int value) { // reallocate if necessary if (count + 1 == allocated) { int[] newValues = new int[allocated * 2]; for (int index = 0; index < count; index++) { newValues[index] = values[index]; } allocated *= 2; } values[count] = value; count++; revision++; } public int Count { get { return(count); } } void CheckIndex(int index) { if (index >= count) throw new ArgumentOutOfRangeException("Index value out of range"); } public int this[int index] { get { CheckIndex(index); return(values[index]); } set { CheckIndex(index); values[index] = value; revision++; } } public IEnumerator GetEnumerator() { return(new IntListEnumerator(this)); } internal int Revision { get { return(revision); } } } class IntListEnumerator: IEnumerator { IntList intList; int revision; int index; internal IntListEnumerator(IntList intList) { this.intList = intList; Reset(); } public bool MoveNext() { index++; if (index >= intList.Count) return(false); else return(true); } public object Current { get { if (revision != intList.Revision) throw new InvalidOperationException("Collection modified while enumerating."); return(intList[index]); } } public void Reset() { index = -1; revision = intList.Revision; } } public class EnumeratorsandForeach { public static void Main() { IntList list = new IntList(); list.Add(1); list.Add(55); list.Add(43); foreach (int value in list) { Console.WriteLine("Value = {0}", value); } foreach (int value in list) { Console.WriteLine("Value = {0}", value); list.Add(124); } } }
Demonstrate IDictionaryEnumerator
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate IDictionaryEnumerator. using System; using System.Collections; public class IDicEnumDemo { public static void Main() { // Create a hash table. Hashtable ht = new Hashtable(); // Add elements to the table ht.Add("Tom", "555-3456"); ht.Add("Mary", "555-9876"); ht.Add("Todd", "555-3452"); ht.Add("Ken", "555-7756"); // Demonstrate enumerator IDictionaryEnumerator etr = ht.GetEnumerator(); Console.WriteLine("Display info using through Entry."); while(etr.MoveNext()) Console.WriteLine(etr.Entry.Key + ": " + etr.Entry.Value); Console.WriteLine(); Console.WriteLine("Display info using Key and Value directly."); etr.Reset(); while(etr.MoveNext()) Console.WriteLine(etr.Key + ": " + etr.Value); } }
Demonstrate an enumerator
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate an enumerator.
using System;
using System.Collections;
public class EnumeratorDemo {
public static void Main() {
ArrayList list = new ArrayList(1);
for(int i=0; i < 10; i++) list.Add(i); // Use enumerator to access list. IEnumerator etr = list.GetEnumerator(); while(etr.MoveNext()) Console.Write(etr.Current + " "); Console.WriteLine(); // Re-enumerate the list. etr.Reset(); while(etr.MoveNext()) Console.Write(etr.Current + " "); Console.WriteLine(); } } [/csharp]
A public enum in one class from another class
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Enum.cs - Demonstrates using a public enum in one class from // another class // // Compile this program with the following command line: // C:>csc enum.cs // namespace nsEnum { using System; public class Enum { // Define the enum type public enum Weekdays { Sun, Mon, Tues, Wed, Thurs, Fri, Sat, Count } static public void Main () { clsSecond second = new clsSecond(); second.ShowEnum (); } } class clsSecond { public void ShowEnum() { // Use the class name with the enum name Console.WriteLine ("Tuesday is day {0} in the week", (int) Enum.Weekdays.Tues); } } }
how a derived class may override an existing enumeration in a base class
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // BaseEnum.cs - Demonstrates how a derived class may override an existing // enumeration in a base class // // Compile this program with the following command line: // C:>csc baseenum.cs // namespace nsEnum { using System; public class clsBaseClass { // Define the enum type public enum Weekdays { Sun, Mon, Tues, Wed, Thurs, Fri, Sat } static public void Main () { Console.WriteLine ("In base class, Sunday = " + (int) Weekdays.Sun); clsNewClass second = new clsNewClass(); second.ShowEnum (); } } class clsNewClass : clsBaseClass { public new enum Weekdays { Sun = 1, Mon, Tues, Wed, Thurs, Fri, Sat } public void ShowEnum() { // Use the class name with the enum name Console.WriteLine ("In derived class, Sunday = " + (int) Weekdays.Sun); } } }
Illustrates the use of an enumeration that defines the orbital periods of the first four planets in days, using a base type of long
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example2_12.cs illustrates the use of an enumeration that defines the orbital periods of the first four planets in days, using a base type of long */ public class Example2_121 { enum PlanetPeriods :long { Mercury = 88, Venus = 225, Earth = 365, Mars = 687 } public static void Main() { System.Console.WriteLine("Orbital period for Mars = " + (long) PlanetPeriods.Mars + " days"); } }
Illustrates the use of an enumeration that defines the orbital periods of the first four planets in days
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example2_11.cs illustrates the use of an enumeration that defines the orbital periods of the first four planets in days */ public class Example2_111 { enum PlanetPeriods { Mercury = 88, Venus = 225, Earth = 365, Mars = 687 } public static void Main() { System.Console.WriteLine("Orbital period for Mars = " + (int) PlanetPeriods.Mars + " days"); } }