using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
IEnumerable<char> query = "Not what you might expect";
foreach (char vowel in "aeiou")
query = query.Where(c => c != vowel);
foreach (char c in query) Console.Write(c);
}
}
Class Interface
Extends IEnumerable
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
public class Employee {
public int currAge;
public string fName, lName;
public Employee() { }
public Employee(string firstName, string lastName, int age) {
currAge = age;
fName = firstName;
lName = lastName;
}
public override string ToString() {
return string.Format("{0}, {1} is {2} years old",
lName, fName, currAge);
}
}
public class PeopleCollection : IEnumerable {
private ArrayList arPeople = new ArrayList();
public PeopleCollection() { }
public Employee GetEmployee(int pos) { return (Employee)arPeople[pos]; }
public void AddEmployee(Employee p) { arPeople.Add(p); }
public void ClearPeople() { arPeople.Clear(); }
public int Count { get { return arPeople.Count; } }
IEnumerator IEnumerable.GetEnumerator() { return arPeople.GetEnumerator(); }
}
class Program {
static void Main(string[] args) {
PeopleCollection myPeople = new PeopleCollection();
myPeople.AddEmployee(new Employee("Homer", "Simpson", 40));
myPeople.AddEmployee(new Employee("Marge", "Simpson", 38));
myPeople.AddEmployee(new Employee("Lisa", "Simpson", 9));
myPeople.AddEmployee(new Employee("Bart", "Simpson", 7));
myPeople.AddEmployee(new Employee("Maggie", "Simpson", 2));
foreach (Employee p in myPeople)
Console.WriteLine(p);
}
}
iterates a collection forward and reverse
using System;
using System.Collections.Generic;
public class Starter {
public static void Main() {
Console.WriteLine("Forward List");
MyClass obj = new MyClass();
foreach (int item in obj) {
Console.Write(item);
}
Console.WriteLine("
Reverse List");
foreach (int item in obj.Reverse) {
Console.Write(item);
}
}
}
public class MyClass {
private int[] list1 = new int[] { 0, 2, 4, 6, 8 };
public IEnumerator<int> GetEnumerator() {
for (int index = 0; index < 5; ++index) {
yield return list1[index];
}
}
public IEnumerable<int> Reverse {
get {
for (int index = 4; index >= 0; --index) {
yield return list1[index];
}
}
}
}
Return IEnumerable by yield
using System;
using System.Collections;
public class Starter {
public static void Main() {
foreach (string day in ToEndOfMonth()) {
Console.WriteLine(day);
}
}
public static IEnumerable ToEndOfMonth() {
DateTime date = DateTime.Now;
int currMonth = date.Month;
while (currMonth == date.Month) {
string temp = currMonth.ToString() + "/" + date.Day.ToString();
date = date.AddDays(1);
yield return temp;
}
}
}
return IEnumerable
using System;
using System.Collections.Generic;
class IteratorDemo {
static IEnumerable<int> OneTwoThree() {
Console.WriteLine("Returning 1");
yield return 1;
Console.WriteLine("Returning 2");
yield return 2;
Console.WriteLine("Returning 3");
yield return 3;
}
static void Main() {
foreach (var number in OneTwoThree()) {
Console.WriteLine(number);
}
}
}
yield return
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class Program {
public static IEnumerable<string> SimpleList() {
yield return "1";
yield return "2";
yield return "3";
}
public static void Main(string[] args) {
foreach (String item in SimpleList())
Console.WriteLine(item);
}
}
The Dispose Pattern
using System;
public class MyClass {
private string name;
public MyClass(string name) { this.name = name; }
override public string ToString() { return name; }
~MyClass() { Console.WriteLine("~MyClass()"); }
public void Dispose() {
Console.WriteLine("Dispose()");
}
}
public class GarbageDisposalApp {
public static void Main(string[] args) {
DoSomething();
Console.WriteLine("end of Main");
}
public static void DoSomething() {
MyClass t = new MyClass("Foo");
Console.WriteLine(t);
t.Dispose();
t = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}