generic programming & collection

23
GENERIC PROGRAMMING & COLLECTION Adhatus Solichah A.

Upload: arya

Post on 24-May-2015

467 views

Category:

Technology


0 download

DESCRIPTION

Coba tok

TRANSCRIPT

Page 1: Generic Programming & Collection

GENERIC PROGRAMMING &

COLLECTIONAdhatus Solichah A.

Page 2: Generic Programming & Collection

Course Structure

Interfaces of the System.Collections Generic methods, class, base class,

interface

Page 3: Generic Programming & Collection

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

Page 4: Generic Programming & Collection

System.Collections

contain types in a more flexible container

The System.Collections namespace defines a number of interfaces

Page 5: Generic Programming & Collection

Interfaces of System.Collections

System.Collections Interface

Meaning

ICollection Defines general characteristics (e.g., size, enumeration, threadsafety) for all non-generic collection types.

IComparer Allows two objects to be compared.

IDictionary Allows a non-generic collection object to represent its contentsusing name/value pairs.

IDictionaryEnumerator

Enumerates the contents of a type supporting IDictionary.

IEnumerable Returns the IEnumerator interface for a given object.

IEnumerator Enables foreach style iteration of subtypes.

IHashCodeProvider Returns the hash code for the implementing type using acustomized hash algorithm.

IList Provides behavior to add, remove, and index items in a list ofobjects. Also, this interface defines members to determinewhether the implementing collection type is read-only and/ora fixed-size container.

Page 6: Generic Programming & Collection

System.Collections Interface Hierarchy

Page 7: Generic Programming & Collection

ICollection

The most primitive interface of System.Collections ICollection extends IEnumerable Provide small set of member to determine:

a. the number of items in the container,b. the thread safety of the container,c. 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);}

Page 8: Generic Programming & Collection

IDictionary

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.

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);}

Page 9: Generic Programming & Collection

IDictionaryEnumerator

IDictionaryEnumerator is simply a strongly typed enumerator, given that it extends IEnumerator

IDictionaryEnumerator allows to enumerate over items in the dictionary via the 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; }}

Page 10: Generic Programming & Collection

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);}

Page 11: Generic Programming & Collection

Classes of System.CollectionsSystem.Col

lections Class

Meaning Key Implemented Interfaces

ArrayList Represents a dynamically sized array of objects.

IList, ICollection, IEnumerable, and ICloneable

Hashtable Represents a collection of objects identified by a numerical key. Custom types stored in a Hashtable should always override System.Object.GetHashCode().

IDictionary, ICollection, IEnumerable, and ICloneable

Queue Represents a standard first-in, first-out (FIFO) queue.

ICollection, ICloneable and IEnumerable

SortedList Like a dictionary; however, the elements can also be accessed by ordinal position (e.g., index).

IDictionary, ICollection, IEnumerable, and ICloneable

Stack A last-in, first-out (LIFO) queue providing push and pop (and peek) functionality.

ICollection, ICloneable, and IEnumerable

Page 12: Generic Programming & Collection

Working with the ArrayList Type making use of the AddRange() method

to populate ArrayList Insert() allows to plug a new item into

the ArrayList at a specified index. (zero based index)

the call to the ToArray() method returns an array of System.Object types based on the contents of the original ArrayList.

Page 13: Generic Programming & Collection

Working with the Queue Type Queues are containers that ensure items

are accessed using a first-in, first-out manner

Member of System.Collections.Que

ue

Meaning

Dequeue() Removes and returns the object at the beginning of the Queue

Enqueue() Adds an object to the end of the Queue

Peek() Returns the object at the beginning of the Queue without removing it

Page 14: Generic Programming & Collection

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()

Page 15: Generic Programming & Collection

Boxing & Unboxing

Boxing Value type Ref type Implicit

Unboxing Ref type Value type Explicit

// 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;

Page 16: Generic Programming & Collection

Boxing & Unboxing

1. A new object must be allocated on the managed heap.

2. The value of the stack-based data must be transferred into that memory location.

3. When unboxed, the value stored on the heap-based object must be transferred back to the stack.

4. The now unused object on the heap will (eventually) be garbage collected.

Page 17: Generic Programming & Collection

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

Page 18: Generic Programming & Collection

::Non-generic::

class Bucket{public string items;

}

::Generic::

class Bucket<T>{public T item;public void Add(T value);public T GetItem();

}

Page 19: Generic Programming & Collection

Classes of System.Collections.Generic

Nongeneric Counterpart

Generic Class in System.Collections

Meaning

Collection<T> CollectionBase The basis for a generic collection

Comparer<T> Comparer Compares two generic objects for equality

Dictionary<TKey, TValue>

Hashtable A generic collection of name/value pairs

List<T> ArrayList A dynamically resizable list of items

Queue<T> Queue A generic implementation of a first-in, first-out (FIFO) list

SortedDictionary<TKey, TValue>

SortedList A generic implementation of a sorted set of name/value pairs

Stack<T> Stack A generic implementation of a last-in, first-out (LIFO) list

LinkedList<T> N/A A generic implementation of a doubly linked list

ReadOnlyCollection<T>

ReadOnlyCollectionBase

A generic implementation of a set of read-only items

Page 20: Generic Programming & Collection

Limitations of Custom Generic Collections

Generic Constraint Meaning

where T : struct The type parameter <T> must have System.ValueType in its chain of inheritance

where T : class The type parameter <T> must not have System.ValueType in its chain of inheritance (e.g., <T> must be a reference type)

where T : new() The type parameter <T>must have a default constructor. This is very helpful if your generic type must create an instance of the type parameter, as you cannot assume the format of custom constructors. Note that this constraint must be listed last on a multiconstrained type

where T : NameOfBaseClass

The type parameter <T> must be derived from the class specified by NameOfBaseClass.

where T : NameOfInterface

The type parameter <T> must implement the interface specified by NameOfInterface.Multiple interfaces can be separated as a comma-delimited list.

Possible Constraints for Generic Type Parameters

Page 21: Generic Programming & Collection

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 type parameter when deriving from a// generic base class.public class MyStringList : MyList<string>{}//orpublic class MyStringList<T> : MyList<T>{}

Page 22: Generic Programming & Collection

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);}

Page 23: Generic Programming & Collection

Next.......

Delegate, Events and Lambdas