You're here: Snippet Directory » Microsoft .NET (152)
Language:

IEnumerable Beispiel

Language: Deutsch
Programming Language: C#
Published by: Chris [not registered]
Last Update: 5/5/2006
Views: 1330

Description

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 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS