c# ienumerable of t code example
Example 1: c# loop through list
using System;
using System.Collections.Generic;
namespace forgetCode {
class program {
public static void Main() {
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
foreach (int item in list) {
Console.WriteLine(item);
}
for (int i = 0; i < list.Count; i++) {
Console.WriteLine(list[i]);
}
}
}
}
Example 2: c# implement ienumerable t
using System.Collections;
class MyObjects : IEnumerable<MyObject>
{
List<MyObject> mylist = new List<MyObject>();
public MyObject this[int index]
{
get { return mylist[index]; }
set { mylist.Insert(index, value); }
}
public IEnumerator<MyObject> GetEnumerator()
{
return mylist.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}