Event-Driven Programming in C# PDF
Document Details
Tags
Summary
This document provides an overview of event-driven programming concepts in C#, including delegates, events, and collections like List, Stack, Queue, and Hashtable.
Full Transcript
Delegates and Events ==================== \ delegate \ \ (\). ================================================================================ public delegate int mathOp(int x, int y); public delegate void errorMessage(); ============================================================================...
Delegates and Events ==================== \ delegate \ \ (\). ================================================================================ public delegate int mathOp(int x, int y); public delegate void errorMessage(); ============================================================================== - GetAnswer mdAdd = new GetAnswer(Formula.getSum); ================================================ - **GetAnswer mdAdd = Formula.Addition;** MessageBox.Show(delegateAddition(10,20).ToString()); ==================================================== public delegate X DisplayOutput\(X arg); ============================================ ![](media/image3.png) -- -- -- -- ![](media/image7.png) public event delegate\_name event\_name. ======================================== event\_name += delegate\_instance;. =================================== ![](media/image9.png) ![](media/image11.png) Collections and Generics ======================== - **Standard collections** -- These are found in the System.Collections namespace. - **Generic collections** -- These are found in the System.Collections.Generic namespace. ArrayList ========= Hashtable ========= SortedList ========== - **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. There are few methods that are used in the SortedList: - **void Add(object key, object value)** -- It adds an item with the specified key and value into the - **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. ![](media/image19.png) Stack ===== Queue ===== ![](media/image23.png) List\ ========= - It is agenericcollection that provides an efficient and dynamically allocated array, which is commonly used to store a list of duplicate objects. The **List\** class can grow and shrink with the number of objects. - The syntax below is the format for creating a **List\** collection. See *Figure 10* for sample program and *Figure 11* for the output. List\ variableName = new List\(); ========================================= - There are few methods that be used in List Collection, see *Table 1* for the descriptions of each method. +-----------------------------------+-----------------------------------+ | **Method** | | +===================================+===================================+ | **Add()** | This method is used to add items | | | and is placed to the end of a | | | list. The elementscan | | | | | | be accessed by calling its index | | | value. | | | | | | **Example:** | | | | | | //Adds a string \"Rose\" | | | nameOfStud.Add(\"Rose\"); | | | | | | //Display the element by index | | | number | | | 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.Itwill | | | return its index value if it is | | | found; if not, it will return the | | | value of -1. | | | | | | **Example:** | | | | | | Console.WriteLine(nameOfStud.Inde | | | xOf(\"Rose\")); | +-----------------------------------+-----------------------------------+ | **Sort()** | This method is used to sort the | | | element in the list object. | | | | | | **Example:** | | | | | | nameOfStud.Sort(); | +-----------------------------------+-----------------------------------+ Queue\ ========== - 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\** generic collection. See *Table 2* for the descriptions of each method. - The syntax below defines a generic queue. See *Figure 12* for sample program and *Figure 13* for the output: Queue\ ageQueue = new Queue\(); =========================================== +-----------------------------------+-----------------------------------+ | **Method Description** | | +===================================+===================================+ | **Enqueue()** | This method adds an element to | | | the end of queue. | | | | | | **Example:** | | | ageQueue.Enqueue(18); | +-----------------------------------+-----------------------------------+ +-----------------------------------+-----------------------------------+ | **Method** | | +===================================+===================================+ | **Peek()** | This will return the element at | | | the beginning of the queue | | | without removing it. | | | | | | **Example:** ageQueue.Peek(); | +-----------------------------------+-----------------------------------+ | **Dequeue()** | This method removes and returns | | | the value at the beginning of the | | | queue. | | | | | | **Example:** int getValue = | | | ageQueue.Dequeue(); | +-----------------------------------+-----------------------------------+ ![](media/image30.png) Stack\ ========== - This generic collection represents the Last In, First Out (LIFO) collection of instances. - There are methods that can be used with **Stack\** generic collection. See *Table 3* for the descriptions of each method. - The syntax below defines the generic stack. See *Figure 14* for sample program and *Figure 15* for the output: Stack\ ageStack = new Stack\(20); ============================================= ------------ -- **Push()** **Peek()** **Pop()** ------------ -- ![](media/image34.png)