Sajmon

Example of IEnumerable

May 9th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. /**
  7. @Author: Sajmon
  8. */
  9. namespace Practise2
  10. {
  11.     public class Student
  12.     {
  13.         private String name;
  14.         private int age;
  15.  
  16.         public Student(String name, int age) {
  17.             this.name = name;
  18.             this.age = age;
  19.         }
  20.  
  21.         public String GetName()
  22.         {
  23.             return this.name;
  24.         }
  25.  
  26.         public int GetAge()
  27.         {
  28.             return this.age;
  29.         }
  30.  
  31.     }
  32. }
  33.  
  34.  
  35.  
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.Text;
  40. using System.Collections;
  41.  
  42. /**
  43. @Author: Sajmon
  44. */
  45. namespace Practise2
  46. {
  47.     public class StudentList : IEnumerable {
  48.  
  49.         private List<Student> students;
  50.         private int index;
  51.  
  52.  
  53.         public StudentList()
  54.         {
  55.             this.students = new List<Student>();
  56.             this.index = -1;
  57.            
  58.         }
  59.  
  60.         public IEnumerator GetEnumerator() {
  61.  
  62.             return (students as IEnumerable).GetEnumerator();
  63.         }
  64.  
  65.  
  66.         public void Add(Student item)
  67.         {
  68.             this.students.Add(item);
  69.         }
  70.  
  71.  
  72.         public Student Current {
  73.             get
  74.             {
  75.                 return this.students[index];
  76.             }
  77.         }
  78.  
  79.  
  80.         public void Reset()
  81.         {
  82.             this.index = -1;
  83.         }
  84.  
  85.  
  86.         public Boolean HasNext() {
  87.             index++;
  88.             if (index < students.Count)
  89.             {
  90.                 return true;
  91.             }
  92.             else
  93.             {
  94.                 return false;
  95.             }
  96.         }
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. using System;
  103. using System.Collections.Generic;
  104. using System.Linq;
  105. using System.Text;
  106. using Practise2;
  107.  
  108. /**
  109. @Author: Sajmon
  110. */
  111. namespace Program
  112. {
  113.     class Program
  114.     {
  115.         static void Main(string[] args)
  116.         {
  117.             StudentList students = new StudentList();
  118.  
  119.             Student student = new Student("Peter Bednar", 28);
  120.             Student student2 = new Student("Jiri Walder", 34);
  121.             Student student3 = new Student("Lumir Navrat", 30);
  122.             Student student4 = new Student("Michal Kratky", 42);
  123.             Student student5 = new Student("Radoslav Fasuga", 35);
  124.  
  125.             students.Add(student);
  126.             students.Add(student2);
  127.             students.Add(student4);
  128.             students.Add(student5);
  129.             students.Add(student3);
  130.  
  131.             while (students.HasNext()) {
  132.                 Console.WriteLine(students.Current.GetName() + " " + students.Current.GetAge());      
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment