Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- class myList<T> : IEnumerable<T>
- {
- T[] list;
- int index = -1;
- public void Add(T obj)
- {
- if (++index < list.Length)
- {
- list[index] = obj;
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- return new TEnum<T>(list);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- public myList(int size)
- {
- list = new T[size];
- }
- }
- //Implement IEnumerator
- class TEnum<T> : IEnumerator<T>
- {
- T[] _list;
- int index = -1;
- public TEnum(T[] objs)
- {
- _list = objs;
- }
- //Return if foreach can iterate to next index or not
- public bool MoveNext()
- {
- return (++index < _list.Length);
- }
- public void Reset()
- {
- index = -1;
- }
- //Get type-safe value of current array's index
- //Its the Implementation of IEnumerator<T>
- public T Current
- {
- get
- {
- return _list[index];
- }
- }
- //It's the implementation of 'IEnumerator'
- object IEnumerator.Current
- {
- get
- {
- //return T Current
- return this.Current;
- }
- }
- //It's the implementation of IDispose interface
- public void Dispose()
- {
- //Write code to dispose un-needed resource
- }
- }
- class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- myList<Person> people = new myList<Person>(3);
- people.Add(new Person { Name = "Ali", Age = 22 });
- people.Add(new Person { Name = "Sundus", Age = 21 });
- people.Add(new Person { Name = "Hogi", Age = 12 });
- foreach (var item in people)
- {
- //No need to cast
- Console.WriteLine("Name:{0} Age:{1}", item.Name, item.Age);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement