Generic Programming & Collection
Upcoming SlideShare
Loading in...5

Generic Programming & Collection



Coba tok

Coba tok



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Generic Programming & Collection Generic Programming & Collection Presentation Transcript

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