AM

Event Driven - Prelim

Delegates - Delegate is a class in .NET that encapsulates a method. It is also a reference type data type that holds a reference method. This class can call any method as long as its signature matches.

delegate keyword - is used in declaring a delegate on a class.

To declare a Delagate:

<access modifier> delegate <return type> <delegate-name> (<parameters>)

To instantiate the delegate, use the new keyword that is associated with the method and supposed to refer on the object.

Example:

• GetAnswer mdAdd = new GetAnswer(Formula.getSum);

• GetAnswer mdAdd = Formula.Addition;

Generic delegates - are not bound to any specific type. These can reference a method that returns and takes parameters of different types.

Example: public delegate X DisplayOutput<X>(X arg);

Difference of Delegate and Interface


Delegate: They have no implementation; they are just safe callbacks

and only have a declaration.

Interface: A class that implements an interface can implement all the

methods associated with it.

Delegate: These can reference any method of a class that provides

arguments and return types.

Interface: Using interfaces allows extending some of the objects’

functionality.

Delegate: These are used if a class needs more than one (1)

implementation of the method.

Interface: These can implement a method only once.

Delegate: These are faster to execute but slower to get.
Interface: These are slower in executing but faster to get.

event - is a member of a class that is fired whenever a specific action takes

place.
Event Handlers - The methods that are invoked when an event occurs

When declaring an event, the following syntax should be:

public event delegate_name event_name

The syntax for adding an event is event_name += delegate_instance;

To register a new subscription to an event, use the add accessor.

To unregister the subscription, use the remove accessor.

This group of objects is called collections, which contains lists, linked lists, dictionaries, and arrays to manage collections of objects.

Standard collections – These are found in the System.Collections namespace.

Generic collections – These are found in the System.Collections.Generic namespace.


ArrayList - This is a collections classt hat is known for an ordered collection of objects. This ArrayList is not the same as an array that is static in size. It is known as dynamic since items can be added and removed from the specified index location.
Hashtable -This class stores key or value pairs where the key represents the value in the collection. When accessing the values in Hashtable, the key must be specified.


SortedList - This class is a combination of ArrayList and Hashtable. It stores key or value pairs where the key values sort values. To access the values in SortedListclass,the key or index value must be specified.

In this class, there are some properties that can be used:

Capacity – It gets or sets the capacity of the SortedList.

Count – It gets the count of the number of elements in the SortedList.

Item – It gets and sets the value associated with a specific key in the SortedList.

Keys – These carry the keys in the SortedList.

Values – These carry the values in the SortedList.

Methods in sorted list:
void Add(object key, object value) – It adds an item with the specified key and value into the

SortedList.

void Clear() – It is used to remove all the items in the SortedList.

bool ContainsKey(object key) – If the SortedList contains the specified key, then it will return the Boolean value true.

bool ContainsValue(object value)– If the SortedListcontains the specified value, then it will return the Boolean value true.

object GetByIndex(int index) – This method returns the value of the specified index.

object GetKey(int index) – This method returns the key of the specified index.

void Remove(object key) – In the SortedList, a key that is specified will remove its element.

void RemoveAt(int index) – It is an index that is specified will remove its element in the SortedList.

Stack - This represents a Last In, First Out (LIFO) collection of objects. When inserting and removing objects from the stack, it uses push and pop operations.
Queue - This class represents a First In, First Out (FIFO) collection of objects. Adding and removing objects from the queue class uses an enqueue and dequeue operations.

Interfaces in the System.Collections Namespace

IEnumerable – It is an interface that allows you to loop through elements in a collection.
ICollection – It is an interface that allows you to determine the number of elements in a collections and copy them in a simple array type.

IDictionary–It is an interface that provides a list of elements that are accessible via a key or value rather than an index.

Generics -
This makes the code reusable across different types by creating a template that contains placeholder types. Generics allows specifying the data types of the programming elements when they are actually used in the program.

The System.Collections.Generic namespace - contains several generic collection classes.

Generic Collections - It avoids creation of custom collections for each type in the application. The generic collections are the generic equivalents for the System.Collectionsclasses found in the System.Collections.Genericnamespace.

List<T>

- It is a generic collection that provides an efficient and dynamically allocated array, which is commonly used to store a list of duplicate objects. The List<T> class can grow and shrink with the number of objects.

List<T> variableName = new List<T>();

Method Description

Add() -This method is used to add items and is placed to the end of a list. The elements can be accessed by calling its index value.

Example:

nameOfStud.Add("Rose");

Console.WriteLine(nameOfStud[2]);

Remove() - This method removes the specified item from the list of object. If in case the list contains duplicate data, the method removes the first matching instance.

Example:

nameOfStud.Remove("Mike");

IndexOf() - This method is used to check the specified element in the specified list object. It will return its index value if it is found; if not, it will return the value of -1.

Example:

Console.WriteLine(nameOfStud.IndexOf("Rose"));

Sort() - This method is used to sort the element in the list object.

Example:

nameOfStud.Sort();

Queue<T>

- It is a generic collection that represents a First In, First Out (FIFO) collection of objects.

- There are a few methods that can be used in Queue<T> generic collection. See Table 2 for the descriptions of each method.

Queue<int> ageQueue = new Queue<int>();

Enqueue() - This method adds an element to the end of queue.

Example:ageQueue.Enqueue(18);


Peek() - This will return the element at the beginning of the queue without removing it.

Example: ageQueue.Peek();

Dequeue() This method removes and returnsthe value at the beginning of the queue.

Example: int getValue = ageQueue.Dequeue();

Stack<T>

- This generic collection represents the Last In, First Out (LIFO) collection of instances.

- There are methods that can be used with Stack<T> generic collection. See Table 3 for the descriptions of each

method.

Stack<int> ageStack = new Stack<int>(20);

Push() -This method adds an element at the top in the stack.

Example:ageStack.Push(18);

Peek() -This will return the element at the top of the stack without removing it.

Example:int getValue = ageStack.Peek();

Pop() - This method removes and returnsthe value at the top of the stack.

Example:int getValue = ageStack.Pop();