Successfully reported this slideshow.

Generic Programming & Collection


Published on

Coba tok

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Generic Programming & Collection

  1. 1. GENERIC programming & COLLECTION<br />Adhatus Solichah A.<br />
  2. 2. Course Structure<br />Interfaces of the System.Collections<br />Generic methods, class, base class, interface<br />
  3. 3. System.Array<br />The most primitive container construct<br />Provides services, e.g. reversing, sorting, clearing, and enumerating<br />Has fixed upper limit<br />it does not automatically resize itself as we add or clear items<br />
  4. 4. System.Collections <br />contain types in a more flexible container<br />The System.Collections namespace defines a number of interfaces<br />
  5. 5. Interfaces of System.Collections<br />
  6. 6. System.Collections Interface Hierarchy<br />
  7. 7. ICollection<br />The most primitive interface of System.Collections<br />ICollection extends IEnumerable<br />Provide small set of member to determine:<br />the number of items in the container,<br />thethread safety of the container,<br />the ability to copy the contents into a System.Array type.<br />public interface ICollection : IEnumerable<br />{<br />int Count { get; }<br />bool IsSynchronized { get; }<br />object SyncRoot { get; }<br />void CopyTo(Array array, int index);<br />}<br />
  8. 8. IDictionary<br />public interface IDictionary :<br />ICollection, IEnumerable<br />{<br />bool IsFixedSize { get; }<br />bool IsReadOnly { get; }<br />object this[object key] { get; set; }<br />ICollection Keys { get; }<br />ICollection Values { get; }<br />void Add(object key, object value);<br />void Clear();<br />bool Contains(object key);<br />IDictionaryEnumerator GetEnumerator();<br />void Remove(object key);<br />}<br />collection that maintains a set of name/value pairs<br />IDictionary interface defines a Keys and Values property as well as Add(), Remove(), and Contains() methods.<br />
  9. 9. IDictionaryEnumerator<br />IDictionaryEnumerator is simply a strongly typed enumerator, given that it extends IEnumerator<br />IDictionaryEnumeratorallows to enumerate over items in the dictionary viathe generalized Entry property<br />Ability to traverse the name/value pairs using the Key/Value properties. (sorting)<br />public interface IDictionaryEnumerator : IEnumerator<br />{<br />DictionaryEntry Entry { get; }<br />object Key { get; }<br />object Value { get; }<br />}<br />
  10. 10. IList<br />provides the ability to insert, remove,and index items into (or out of) a container<br />public interface IList :<br />ICollection, IEnumerable<br />{<br />bool IsFixedSize { get; }<br />bool IsReadOnly { get; }<br />object this[ int index ] { get; set; }<br />int Add(object value);<br />void Clear();<br />bool Contains(object value);<br />int IndexOf(object value);<br />void Insert(int index, object value);<br />void Remove(object value);<br />void RemoveAt(int index);<br />}<br />
  11. 11. Classes of System.Collections<br />
  12. 12. Working with the ArrayList Type<br />making use of the AddRange() method to populate ArrayList<br />Insert() allows to plug a new item into theArrayList at a specified index. (zero based index)<br />the call to the ToArray() methodreturns an array of System.Object typesbased on the contents of the original ArrayList.<br />
  13. 13. Working with the Queue Type<br />Queues are containers that ensure items are accessed using a first-in, first-out manner<br />
  14. 14. Working with the Stack Type<br />represents a collection that maintains items using a last-in, first-out manner.<br />Stack defines a member named Push() and Pop()<br />
  15. 15. Boxing & Unboxing<br />Boxing<br />Value type  Ref type<br />Implicit<br />Unboxing<br />Ref type  Value type<br />Explicit<br />// Make a short value type.<br />short s = 25;<br />// Box the value into an object reference.<br />object objShort = s;<br />// Unbox the reference back into a corresponding short.<br />short anotherShort = (short)objShort;<br />
  16. 16. Boxing & Unboxing<br />A new object must be allocated on the managed heap.<br />The value of the stack-based data must be transferred into that memory location.<br />When unboxed, the value stored on the heap-based object must be transferred back to the stack.<br />The now unused object on the heap will (eventually) be garbage collected.<br />
  17. 17. System.Collections.Generic<br />Contains numerous class and interface types that allow you to contain subitems in a variety of containers<br />generic interfaces mimic the corresponding nongeneric types in the System.Collections<br />
  18. 18. ::Non-generic::<br />class Bucket{<br />public string items;<br />}<br />::Generic::<br />class Bucket<T>{<br />public T item;<br />public void Add(T value);<br />public T GetItem();<br />}<br />
  19. 19. Classes of System.Collections.Generic<br />
  20. 20. Limitations of Custom Generic Collections<br />Possible Constraints for Generic Type Parameters<br />
  21. 21. Creating Generic Base Classes<br />generic classes can be the base class to other classes<br />// Assume you have created a custom<br />// generic list class.<br />public class MyList<T><br />{<br />private List<T> listOfData = new List<T>();<br />}<br />// Concrete types must specify the typeparameter when deriving from a<br />// generic base class.<br />public class MyStringList : MyList<string><br />{}<br />//or<br />public class MyStringList<T> : MyList<T><br />{}<br />
  22. 22. Creating Generic Interfaces<br />public interface IBinaryOperations<T> where T : struct<br />{<br />T Add(T arg1, T arg2);<br />T Subtract(T arg1, T arg2);<br />T Multiply(T arg1, T arg2);<br />T Divide(T arg1, T arg2);<br />}<br />
  23. 23. Next.......<br />Delegate, Events and Lambdas<br />