
10266A_13.ppt
- Количество слайдов: 17
Module 13 Building and Enumerating Custom Collection Classes
Module Overview • Implementing a Custom Collection Class • Adding an Enumerator to a Custom Collection Class
Lesson 1: Implementing a Custom Collection Class • What Are Custom Collection Classes? • Generic Collection Interfaces in the. NET Framework • Implementing a Simple Custom Collection Class • Implementing a Dictionary Collection Class
What Are Custom Collection Classes? Sequential List—List<T> 5 8 7 1 0 2 3 4 5 7 8 Ordered List—Sorted. List<T> 0 1 2 Binary Tree – ? ? – Need a custom collection class 4 2 0 7 3 5 8
Generic Collection Interfaces in the. NET Framework ICollection<T> : IList<T> : IDictionary<TKey, TValue> : IEnumerable<T> ICollection<Key. Value. Pair <TKey, TValue>> Methods: • Add • Index. Of • Add • Clear • Insert • Contains. Key • Contains • Remove. At • Get. Enumerator • Copy. To • Remove • Try. Get. Value Properties: • Count • Item • Is. Read. Only • Keys • Values
Implementing a Simple Custom Collection Class public int Count { get {. . . } } class Double. Ended. Queue<T> : ICollection<T>, IList<T> { private List<T> items; public bool Is. Read. Only { get {. . . } } // Type specific methods. public Double. Ended. Queue() {. . . } public bool Remove(T item) {. . . } public IEnumerator<T> Get. Enumerator() {. . . } public Enqueue. Item. At. Start() {. . . } public Deqeueue. Item. From. Start() {. . . } IEnumerator IEnumerable. Get. Enumerator() {. . . } public Enqueue. Item. At. End() {. . . } public int Index. Of(T item) {. . . } public Dequeue. Item. From. End() {. . . } public void Insert (int index, T item) {. . . } // Interface methods. public void Add(T item) {. . . } public void Remove. At(int index) {. . . } public void Clear() {. . . } public bool Contains(T item) {. . . } public void Copy. To(T[] array, int array. Index) {. . . } } public T this[int index] { get {. . . } set {. . . } }
Implementing a Dictionary Collection Class class Intelligent. Dictionary<TKey, TValue> : IDictionary<TKey, TValue> { public Intelligent. Dictionary() {. . . } public void Add(Key. Value. Pair <TKey, TValue> item) {. . . } public void Clear() {. . . } public Tkey Add. Item (Tkey key, Tvalue) {. . . } public bool Contains (Key. Value. Pair<TKey, TValue> item) {. . . } public void Add (TKey key, TValue value) {. . . } public void Copy. To(Key. Value. Pair <TKey, TValue>[] array, int array. Index) {. . . } public bool Contains. Key(TKey key) {. . . } public ICollection<TKey> Keys { get {. . . } } public int Count { get {. . . } } public bool Is. Read. Only { get {. . . } } public bool Remove(Key. Value. Pair <TKey, TValue> item) {. . . } public bool Remove(TKey key) {. . . } public bool Try. Get. Value (TKey key, out TValue value) {. . . } public IEnumerator<Key. Value. Pair <TKey, TValue>> Get. Enumerator() {. . . } public ICollection<TValue> Values { get {. . . } } public TValue this[TKey key] { get {. . . } set {. . . } } } IEnumerator IEnumerable. Get. Enumerator() {. . . }
Lesson 2: Adding an Enumerator to a Custom Collection Class • What Is an Enumerator? • What Is the IEnumerable<T> Interface? • What Is the IEnumerator<T> Interface? • Implementing an Enumerator Manually • Implementing an Enumerator by Using an Iterator
What Is an Enumerator? my. Int. Collection 5 8 7 1 0 foreach (int datum in my. Int. Collection) { // Process datum. } 2 3 Enumerator: 0 1 2 3 4 5 7 8 4
What Is the IEnumerable<T> Interface? The IEnumerable<T> interface is used with collection classes that support enumeration The IEnumerable<T> interface defines the Get. Enumerator method The IEnumerable<T> interface inherits from the IEnumerable interface that also defines a class Custom. Collection. Class<T> : IEnumerable<T> { public IEnumerator<T> Get. Enumerator() { // Implementation not shown. . } IEnumerator IEnumerable. Get. Enumerator() { throw new Not. Implemented. Exception(); } // Additional enumerators return // instances of the IEnumerable<T> // interface. public IEnumerable<T> Backwards() { // Implementation not shown. . } Get. Enumerator method } . . .
What Is the IEnumerator<T> Interface? IEnumerator<T> IEnumerator : IEnumerator, IDisposable Methods: • void Reset Properties: Resets the enumerator to the start of the • T Current collection Returns the current • bool Move. Next item from the Moves the enumerator to the next point in the collection The property is type safe Properties: • object Current Returns the current item from the collection The property is not type safe
Implementing an Enumerator Manually 1 Implement the IEnumerator<T> partial class Custom. Collection. Class<T> : IEnumerator<T> { int pointer= -1; interface 2 public T Current { get { if (pointer!= -1) {return pointer]; } else {throw new Invalid. Operation. Exception(); } } } Define the Current property of type T 3 Define the Move. Next method that advances to back to before the public bool Move. Next() { if (pointer < (vals. Length - 1)) { pointer ++; return true; } else { return false; } } first item public void Reset() { pointer = -1; } the next item in the collection 4 Define the Reset method to move }
Implementing an Enumerator by Using an Iterator An iterator returns (or yields) a sequence of ordered data that the compiler can use to build an enumerator Use a yield return statement to return each item from the collection in order // Assume data is a valid object. public IEnumerable<T> Backwards { get { for (int i = data. Count - 1; i >= 0; i--) { yield return data[i]; } } } • The compiler uses the items that an iterator returns to define a type that implements both the IEnumerator<T> and IEnumerable<T> interfaces, which include the Current property and the Move. Next, Reset, and Get. Enumerator methods • If you use an iterator in a property, you should return an instance of the IEnumerable<T> interface that you can use with foreach statements
Lab: Building and Enumerating Custom Collection Classes • Exercise 1: Implementing the IList<TItem> Interface • Exercise 2: Implementing an Enumerator by Writing Code • Exercise 3: Implementing an Enumerator by Using an Iterator Logon information Virtual machine 10266 A-GEN-DEV User name Student Password Pa$$w 0 rd Estimated time: 90 minutes
Lab Scenario
Lab Review Questions • In the lab, you implemented the IList<T> interface. When would you use the IList<T> interface in a custom class? • In the lab, you exposed a second enumerator to iterate through the collection in reverse order. How do iterators enable you to implement enumerators with minimal code?
Module Review and Takeaways • Review Questions • Best Practices
10266A_13.ppt