public static void TestGetEnumerator() { MyCollection collBase = CreateCollection(100); IEnumerator enumerator = collBase.GetEnumerator(); Assert.NotNull(enumerator); int count = 0; while (enumerator.MoveNext()) { Assert.Equal(collBase[count], enumerator.Current); count++; } Assert.Equal(collBase.Count, count); }
static void Main() { MyCollection collection = new MyCollection(10); foreach (int item in collection) { Console.Write(item + " "); } Console.WriteLine(); //////////////////////////////////////////////// for (AlexEnumerator tmp = collection.GetEnumerator(); tmp.MoveNext();) { int item = tmp.Current; Console.Write(item + " "); } Console.WriteLine(); }
public static void TestGetEnumerator_Invalid() { MyCollection collBase = CreateCollection(100); IEnumerator enumerator = collBase.GetEnumerator(); // Index < 0 Assert.Throws <InvalidOperationException>(() => enumerator.Current); // Index >= dictionary.Count while (enumerator.MoveNext()) { ; } Assert.Throws <InvalidOperationException>(() => enumerator.Current); Assert.False(enumerator.MoveNext()); // Current throws after resetting enumerator.Reset(); Assert.True(enumerator.MoveNext()); enumerator.Reset(); Assert.Throws <InvalidOperationException>(() => enumerator.Current); }
static void Main(string[] args) { Son o1 = new Son(5); Dad o3 = new OtherSon(); Dad o2 = o1; // o1 = o3; // o1 = (Son)o3; o1 = o3 is Son ? (Son)o3 : null; o1 = o3 as Son; o1 = o3 as Son; Console.WriteLine(o1); OtherSon o4 = new OtherSon(); Console.WriteLine("======================"); o2.F(); o4.F(); o3.F(); MyClass[] myObjects = { 5, 4, 3, 2, 1 }; Array.Sort(myObjects); foreach (var o in myObjects) { Console.Write(" " + o.Number); } Console.WriteLine(); //Console.WriteLine(o1.G<char, int>(5)); MyClass o5 = 10; IComparable <MyClass> o6 = o5; Console.WriteLine(o6.CompareTo(new MyClass() { Number = 1 })); MyCollection coll = new MyCollection(); Console.WriteLine("###################"); IEnumerator iter = coll.GetEnumerator(); while (iter.MoveNext()) { Console.Write(iter.Current); } Console.WriteLine(); Console.WriteLine("###################"); foreach (var element in coll) { if (element.Number > 5) { break; } Console.Write(element.Number); } Console.WriteLine(); int counter = 15; foreach (int a in ArithSeq(1, 2)) { if (--counter == 0) { break; } Console.Write(" " + a); } Console.WriteLine(); }
public static void Main(string[] args) { //1* Implement double LinkedList for a Person class (which contains Id, Name , Location properties) and use enumerator to // iterate through each item and print the values on console. Note: Dont not use any default classes, only use either IEnumerable/IEnumerator. Myenumarable enumarableObj = new Myenumarable (); var enumarator = enumarableObj.GetEnumerator (); while (enumarator.MoveNext ()) { Person person = (Person)enumarator.Current; Console.WriteLine ("Person ID: {0}, Name: {1}, Location: {2}",person.ID, person.Name, person.Location); } //2. Implement IList<T> interface and perform operations to add, remove, contains, clear all. MyCollection<int> myCollection = new MyCollection<int>(); myCollection.Add (1); myCollection.Add (2); myCollection.Add (3); var enumarator1 = myCollection.GetEnumerator(); while (enumarator1.MoveNext ()) { Console.WriteLine (enumarator1.Current); } //4. Using .Net provided List<T> and Dictionary<Tkey,TValue>, perform operations for Add,AddRange, Remove, Contains, InsertAt, Clear. MyCollection<string> myCollection4 = new MyCollection<string>(); myCollection4.Add ("John"); myCollection4.Add ("Lessy"); myCollection4.Add ("Clair"); var enumarator4 = myCollection4.GetEnumerator(); while (enumarator4.MoveNext ()) { Console.WriteLine (enumarator4.Current); } myCollection4.Remove ("Lessy"); var enumarator41 = myCollection4.GetEnumerator(); while (enumarator41.MoveNext ()) { Console.WriteLine (enumarator41.Current); } //3* Implement stack and Queue using array as backing field in the class. stack st = new stack(); while (true) { Console.Clear(); Console.WriteLine("\nStack MENU(size -- 10)"); Console.WriteLine("1. Add an element"); Console.WriteLine("2. See the Top element."); Console.WriteLine("3. Remove top element."); Console.WriteLine("4. Display stack elements."); Console.WriteLine("5. Exit"); Console.Write("Select your choice: "); int choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter an Element : "); st.Push(Console.ReadLine()); break; case 2: Console.WriteLine("Top element is: {0}", st.Peek()); break; case 3: Console.WriteLine("Element removed: {0}", st.Pop()); break; case 4: st.Display(); break; case 5: System.Environment.Exit(1); break; } Console.ReadKey(); } }