EkaSe / Calculator

1 stars 1 forks source link

Collections: IMyEnumerator and IMyEnumerable #2

Closed acrm closed 7 years ago

acrm commented 7 years ago
  1. Define IMyEnumerator intarface with following members:
  1. Define IMyEnumerable interface with following member:

    • IMyEnumerator Enumerator {get;} - returns the enumerator for this collection
  2. Create nested class MyListEnumerator, implemented IMyEnumerator for the instance of MyList collection, given in constructor parameter

  3. Implement in the MyList class IMyEnumerable interface

  4. Do the same for MyStack and MyLinkedList classes

acrm commented 7 years ago

Additional Task 1: Tests Make tests for each collection in such form: var myList = new MyList(); myList.Add(1); myList.Add(3); myList.Add(5);

var resultValues = new List(); var enumerator = myList.GetEnumerator(); enumerator.Reset(); while(enumerator.HasNext) { enumerator.Next(); var value = enumerator.Current; var resultValues.Add(value) }

if(resultValues[0] = 1 && resultValues[1] = 3 && resultValues[2] = 5) //pass else //fail

acrm commented 7 years ago

Additional Task 2: Nested classes Move definitions of enumerator classes inside corresponding collection classes and change enumerators classes constructors such way, that now they consume a reference to collection instance itself instead of instaces inner collection. (Nested classes have access to all private members of nesting class)

acrm commented 7 years ago

Additional Task 3: .NET Integration

Overview

Our enumerators work fine, by they don't give us a lot of advantages. By now it's just another way to enumerate collection items and it's actually more noisy that simple use of cycles. May be it gives some level of abstraction over collections but now it's not help us much. But our My-interfaces are just models of built-in IEnumerator and IEnumerable interfaces for .NET standard libraries. And for that built-in interfaces C# language has bult-in supportive syntax, that makes use of classes implemented these interfaces much handy.

In details:

  1. foreach statement can enumerate object of any class, which implements IEnumerable
  2. Collection initialization can initialize object of any class, which implements IEnumerable and contains method Add(item)
  3. yield statement requires that the return value of the method which contains this statement is IEnumerable - we will consider that statement after completion of this task in the Extension class issue.

Task

  1. Inherit IMyEnumerable from IEnumerable and IMyEnumerator from IEnumerator and then implement missing members in corresponding My-classes.
  2. Make an example code in which used coillection initialization and foreach for My-collections. Put this example among tests for MyCollections.

References:

IEnumerable Collection Initializers yield

acrm commented 7 years ago

Nice done! Now can merge to master