Csharp/C Sharp/Class Interface/IEnumerable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
Enumerator Example (Versioned Collection)
using System;
using System.Collections;
public class Starter {
public static void Main() {
MyCollection simple = new MyCollection(new object[] { 1, 2, 3, 4, 5, 6, 7 });
IEnumerator enumerator = simple.GetEnumerator();
enumerator.MoveNext();
Console.WriteLine(enumerator.Current);
enumerator.MoveNext();
simple[4] = 10;
Console.WriteLine(enumerator.Current);
enumerator.MoveNext();
}
}
public class MyCollection : IEnumerable {
public MyCollection(object[] array) {
items = array;
version = 1;
}
public object this[int index] {
get {
return items[index];
}
set {
++version;
items[index] = value;
}
}
public IEnumerator GetEnumerator() {
return new Enumerator(this);
}
private class Enumerator : IEnumerator {
public Enumerator(MyCollection obj) {
oThis = obj;
cursor = -1;
version = oThis.version;
}
public bool MoveNext() {
++cursor;
if (cursor > (oThis.items.Length - 1)) {
return false;
}
return true;
}
public void Reset() {
cursor = -1;
}
public object Current {
get {
if (oThis.version != version) {
throw new InvalidOperationException("Collection was modified");
}
if (cursor > (oThis.items.Length - 1)) {
throw new InvalidOperationException("Enumeration already finished");
}
if (cursor == -1) {
throw new InvalidOperationException("Enumeration not started");
}
return oThis.items[cursor];
}
}
private int version;
private int cursor;
private MyCollection oThis;
}
private object[] items = null;
private int version;
}
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);
}
}
IEnumerable with a foreach loop
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);
}
}
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("\nReverse 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
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);
}
}
}
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;
}
}
}
Tree Enumerator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Tree<TItem> : IEnumerable<TItem> where TItem : IComparable<TItem> {
public Tree(TItem nodeValue) {
this.NodeData = nodeValue;
this.LeftTree = null;
this.RightTree = null;
}
public void Insert(TItem newItem) {
TItem currentNodeValue = this.NodeData;
if (currentNodeValue.rupareTo(newItem) > 0) {
if (this.LeftTree == null) {
this.LeftTree = new Tree<TItem>(newItem);
} else {
this.LeftTree.Insert(newItem);
}
} else {
if (this.RightTree == null) {
this.RightTree = new Tree<TItem>(newItem);
} else {
this.RightTree.Insert(newItem);
}
}
}
public void WalkTree() {
if (this.LeftTree != null) {
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData.ToString());
if (this.RightTree != null) {
this.RightTree.WalkTree();
}
}
public TItem NodeData { get; set; }
public Tree<TItem> LeftTree { get; set; }
public Tree<TItem> RightTree { get; set; }
IEnumerator<TItem> IEnumerable<TItem>.GetEnumerator() {
return new TreeEnumerator<TItem>(this);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
throw new NotImplementedException();
}
}
class TreeEnumerator<T> : IEnumerator<T> where T : IComparable<T> {
public TreeEnumerator(Tree<T> data) {
this.currentData = data;
}
private void populate(Queue<T> enumQueue, Tree<T> tree) {
if (tree.LeftTree != null) {
populate(enumQueue, tree.LeftTree);
}
enumQueue.Enqueue(tree.NodeData);
if (tree.RightTree != null) {
populate(enumQueue, tree.RightTree);
}
}
private Tree<T> currentData = null;
private T currentItem = default(T);
private Queue<T> enumData = null;
T IEnumerator<T>.Current {
get {
if (this.enumData == null)
throw new InvalidOperationException("Use MoveNext before calling Current");
return this.currentItem;
}
}
void IDisposable.Dispose() {
// throw new NotImplementedException();
}
object System.Collections.IEnumerator.Current {
get { throw new NotImplementedException(); }
}
bool System.Collections.IEnumerator.MoveNext() {
if (this.enumData == null) {
this.enumData = new Queue<T>();
populate(this.enumData, this.currentData);
}
if (this.enumData.Count > 0) {
this.currentItem = this.enumData.Dequeue();
return true;
}
return false;
}
void System.Collections.IEnumerator.Reset() {
throw new NotImplementedException();
}
}
class Program {
static void Main(string[] args) {
Tree<int> tree1 = new Tree<int>(10);
tree1.Insert(5);
tree1.Insert(11);
tree1.Insert(5);
tree1.Insert(-12);
tree1.Insert(15);
tree1.Insert(0);
tree1.Insert(14);
tree1.Insert(-8);
tree1.Insert(10);
foreach (int item in tree1)
Console.WriteLine(item);
}
}
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);
}
}