Du bist hier: Snippet-Verzeichnis » Microsoft .NET (154)
Sprache:

IEnumerable Beispiel

Sprache: Deutsch
Programmiersprache: C#
Veröffentlicht von: Chris [nicht registriert]
Letzte Änderung: 05.05.2006
Aufrufe: 1825

Beschreibung

Zeigt wie man ein Objekt "IEnumerable"-ready macht, damit man es durchlaufen kann.

Code

1 using System; 2 using System.Collections; 3 4 5 public class EnumerableArrayList : IEnumerable 6 { 7 ArrayList al; 8 public EnumerableArrayList() 9 { 10 this.al = new ArrayList(); 11 al.Add("Christoph"); 12 al.Add("Christian"); 13 } 14 15 16 public IEnumerator GetEnumerator() 17 { 18 return new MyEnumerator(this.al); 19 } 20 21 22 class MyEnumerator : IEnumerator 23 { 24 ArrayList al; 25 int index; 26 27 28 public MyEnumerator(ArrayList al) 29 { 30 this.al = al; 31 index = -1; 32 } 33 34 35 public object Current { 36 get 37 { 38 return al[index]; 39 } 40 } 41 42 43 public bool MoveNext() 44 { 45 index++; 46 if (index >= al.Count) return false; 47 return true; 48 } 49 50 51 public void Reset() 52 { 53 index = -1; 54 } 55 } 56 } 57 58 59 class MainClass 60 { 61 public static void Main(string[] args) 62 { 63 EnumerableArrayList mal = new EnumerableArrayList(); 64 65 66 foreach (object obj in mal) 67 Console.WriteLine(obj); 68 } 69 }

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS