0% found this document useful (0 votes)
10 views52 pages

Unit 1.6

The document outlines the curriculum for a course on NET Centric Computing at Tribhuvan University, focusing on events and delegates, collections and generics, and file I/O in C#. It covers the concepts of delegates, multicast delegates, events, and various collection types in C#, along with file input/output operations and serialization. The instructor for the course is Tekendra Nath Yogi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views52 pages

Unit 1.6

The document outlines the curriculum for a course on NET Centric Computing at Tribhuvan University, focusing on events and delegates, collections and generics, and file I/O in C#. It covers the concepts of delegates, multicast delegates, events, and various collection types in C#, along with file input/output operations and serialization. The instructor for the course is Tekendra Nath Yogi.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tribhuvan University

INSTUTE OF SCIENCE AND TECHNOLOGY(IOST)


Bachelor in Computer Science and Information Technology (B.SC CSIT)
VI Semester
NET Centric Computing(CSC-367 )
Unit-1.6: Events and Delegates, Collection and
Generics, File IO in C#

Instructor
Tekendra Nath Yogi
[email protected]
Contents
• Delegate and Events
– Delegate and Multicast Delegates

– Events, Multicast Events and custom Events

• Collections and Generics


– Collections in C#

– Generic Collection in C#

• File I/O
– Filestream

– StreamWriter and StreamReader

– TextWriter and TextReader

– BinaryWriter and BinaryReader

– FileInfo

– Serialization and deserialization


7/21/2025 By: Tekendra Nath Yogi 2
Delegates and Events in C#
• A delegate is an object which refers to methods. i.e., it is a reference type
variable that can hold a reference to the methods having same signature as
the delegate.

• It is mainly used to call methods while implementing events.

• To implement the delegate mechanism in the program for calling methods


following three Steps as used:

– Declare the delegate

– Instantiation of delegate

– Invoking a delegate

7/21/2025 By: Tekendra Nath Yogi 3


Contd…,
• Declaration of Delegates:
modifier delegate return_type delegate_name (parameter_list);

e.g., public delegate void TestDelegate (int a, int b);

7/21/2025 By: Tekendra Nath Yogi 4


Contd…,
• Instantiating the delegate: Create an instance and reference to a method

– For Instance methods


delegate_name instance_name = new delegate_name(Object_Name.calling_method_name);

e.g. TestDelegate d = new TestDelegate(O.Add);

or

delegate_name instance_name=Object_name.calling_method_name;

e.g., TestDelegate d =O.Add;

7/21/2025 By: Tekendra Nath Yogi 5


Contd…,
• Instantiating the delegate:
For Static methods:
delegate_name instance_name = new delegate_name(Class_Name.calling_method_name);

e.g. TestDelegate d = new TestDelegate(Class_A.Add);

or

delegate_name instance_name= Class_Name.calling_method_name;

e.g., TestDelegate d =ClassA.Add;

7/21/2025 By: Tekendra Nath Yogi 6


Contd…,
• Invoking a delegate:

Syntax: delegate_Instance_name( args );

e.g., d(10, 20);

Or

Syntax: delegate_Instance_name.Invoke (args);

e.g., d.Invoke(10,20);

7/21/2025 By: Tekendra Nath Yogi 7


Contd…,

7/21/2025 By: Tekendra Nath Yogi 8


Contd…,
• Multicast Delegate:

– The delegate can point to multiple methods.

– A delegate that points multiple methods is called a multicast delegate.

– The "+" or "+=" operator adds a function to the invocation list, and

– The "-" and "-=" operator removes it.

7/21/2025 By: Tekendra Nath Yogi 9


Contd…,

7/21/2025 By: Tekendra Nath Yogi 10


Events
• An event is a notification sent by an object to signal the occurrence of an
action.

• The class who raises events is called Publisher, and the class who receives the
notification is called Subscriber. There can be multiple subscribers of a single
event.

• Typically, a publisher raises an event when some action occurred. The


subscribers, who are interested in getting a notification when an action
occurred, should register with an event and handle it.

7/21/2025 By: Tekendra Nath Yogi 11


Contd…,
• In C#, an event is an encapsulated delegate. The following figure illustrates
the event in C#.

7/21/2025 By: Tekendra Nath Yogi 12


Contd…,
• Declare the delegate in the publisher class as:
– Syntax: modifier delegate return_type Delegate_Name(args);

– E.g.: public delegate void TestDelegate();

– Note: Delegate signature should be same as the event handler method in the event
subscriber class.

7/21/2025 By: Tekendra Nath Yogi 13


Contd…,
• Declare the event in the publisher class as:

– Syntax: modifier event Delegate_name Event_Name;

– E.g., public event TestDelegate TestEvent;

7/21/2025 By: Tekendra Nath Yogi 14


Contd…,
• Define the method that raise an event when some action performed in the
publisher class:

protected virtual void OnTestEvent()

// if TestEvent isnot null then invoke delegate i.e. event

TestEvent?.Invoke( );

This method is called when an action has performed in a publisher class and
this method raise event to be handled by the event handle.

7/21/2025 By: Tekendra Nath Yogi 15


Contd…,
• Define the event subscriber class with event handler method having signature
as the delegate declared in publisher class :

Class Subscriber_Class_Name

modifier return type Event_Handler_method_Name(args)

// body with event handling code

7/21/2025 By: Tekendra Nath Yogi 16


Contd…,
• Register the Event handler method present in the subscriber class with the
Event as:
• Publisher_class_object.Event_Name+= Subscriber_class_object.Event_Handler_Method_Name;

7/21/2025 By: Tekendra Nath Yogi 17


Contd…,
• Example:

7/21/2025 By: Tekendra Nath Yogi 18


Contd…,
• Multi-Cast Events:

7/21/2025 By: Tekendra Nath Yogi 19


Contd…,

7/21/2025 By: Tekendra Nath Yogi 20


• Custom Event with data: Contd…,

7/21/2025 By: Tekendra Nath Yogi 21


Contd…,

7/21/2025 By: Tekendra Nath Yogi 22


Collections and Generics
• Collections refers to the series of values or objects.

• In C # to represent such collections provides classes. Categorized as:

– Non-Generic collection :
• defined in the namespace System.Collections

– Generic collections:
• defined in the namespace System.Collections.Genrics namespace.

7/21/2025 By: Tekendra Nath Yogi 23


Contd…,
ArrayList
SortedList
Non Generic Stack
collections
Queue
Hashtable
BitArray
Collections
List<T>

Dictionary<Tkey,, Tvalue>

SortedList<Tkey, Tvalue>
Generic
collections Queue<T>
Stack<T>
Hashset<T>
7/21/2025 By: Tekendra Nath Yogi 24
Contd…,

7/21/2025 By: Tekendra Nath Yogi 25


Contd…,

7/21/2025 By: Tekendra Nath Yogi 26


Collections --ArrayList
• In C#, the ArrayList is a non-generic collection of objects whose size increases
dynamically. It is the same as Array except that its size increases dynamically.

• An ArrayList can be used to add unknown data where you don't know the types
and the size of the data.

7/21/2025 By: Tekendra Nath Yogi 27


Contd…,
• Example:

7/21/2025 By: Tekendra Nath Yogi 28


Contd…,
• ArrayList Property:

7/21/2025 By: Tekendra Nath Yogi 29


Contd…,
• ArrayList Methods:

7/21/2025 By: Tekendra Nath Yogi 30


Collections --Stack

7/21/2025 By: Tekendra Nath Yogi 31


Collections --Queue

7/21/2025 By: Tekendra Nath Yogi 32


Collections --Hashtable

7/21/2025 By: Tekendra Nath Yogi 33


Generics – List<T>

7/21/2025 By: Tekendra Nath Yogi 34


Generics – Stack<T>

7/21/2025 By: Tekendra Nath Yogi 35


Generics – Queue<T>

7/21/2025 By: Tekendra Nath Yogi 36


File I/O
• A file is a collection of data stored on a storage media such as disk.

• File I/O refers to the processes involved in reading from and writing to
files.

• It includes operations such as : reading, writing, appending, deleting,


closing, etc.

• It is required for the persistency of the data.

• In C# for file I/O various classes are available such as: Filestream,
StreamReader, StreamWriter, TextReader, TextWriter, BinaryReader,
BinaryWriter, FileInfo, etc.

7/21/2025 By: Tekendra Nath Yogi 37


File I/O --FileStream

7/21/2025 By: Tekendra Nath Yogi 38


Contd…,
• File directory state before execution of the program:
File content after execution of the program

• File directory state after execution of the program


Console output after reading file in the program

7/21/2025 By: Tekendra Nath Yogi 39


File I/O –StreamWriter and StreamReader

7/21/2025 By: Tekendra Nath Yogi 40


Contd…,
• File directory state before execution of the program: File content after execution of the program

• File directory state after execution of the program Console output after reading file in the program

7/21/2025 By: Tekendra Nath Yogi 41


File I/O –TextWriter and TextReader

7/21/2025 By: Tekendra Nath Yogi 42


Contd…,
• File directory state before execution of the program:
File content after execution of the program

• File directory state after execution of the program Console output after reading file in the program

7/21/2025 By: Tekendra Nath Yogi 43


File I/O –BinaryWriter and BinaryReader

7/21/2025 By: Tekendra Nath Yogi 44


Contd…,
• File directory state before execution of the program:
File content after execution of the program

• File directory state after execution of the program Console output after reading file in the program

7/21/2025 By: Tekendra Nath Yogi 45


File I/O –FileInfo

7/21/2025 By: Tekendra Nath Yogi 46


Contd…,
• File directory state before execution of the File content after execution of the program

program:

• File directory state after execution of the


program

Console output after reading file in the program

7/21/2025 By: Tekendra Nath Yogi 47


Serialization and Deserialization
• Serialization is the process of write object into the file.

7/21/2025 By: Tekendra Nath Yogi 48


Contd…,
• Deserialization is the process of reading object from the file into the program.

7/21/2025 By: Tekendra Nath Yogi 49


Contd…,

7/21/2025 By: Tekendra Nath Yogi 50


Contd…,
• File directory state before execution of the program:
File content after execution of the program

• File directory state after execution of the program Console output after reading file in the program

7/21/2025 By: Tekendra Nath Yogi 51


Thank You !

7/21/2025 By: Tekendra Nath Yogi 52

You might also like