0% found this document useful (0 votes)
6 views31 pages

Unit 3

Uploaded by

nrpcespce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

Unit 3

Uploaded by

nrpcespce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Unit – 3

C#.NET
Outline

 Language Features and Creating .NET


Projects,
 Namespaces Classes and Inheritance –C
 Exploring the Base Class Library
 Debugging and Error Handling – Data Types
 Exploring Assemblies and Namespaces
 String Manipulation
 Files and I/O
 Collections
December 12, 2024 Prepared By : Mittal Rayjada 2
Language features
 Boolean Conditions
 Automatic Garbage Collection
 Standard Library
 Assembly Versioning
 Properties and Events
 Delegates and Events Management
 Easy-to-use Generics
 Indexers
 Conditional Compilation
 Simple Multithreading
 LINQ and Lambda Expressions
 Integration with Windows
December 12, 2024 Prepared By : Mittal Rayjada 3
Debugging & Error Handling
 An exception/error is a anomalies that arises during the
execution of a program.
 A C# error is a response to an exceptional circumstance
that arises while a program is running, such as an
attempt to divide by zero.
 Exception handling is a built-in mechanism in .NET
framework to detect and handle run time errors.
 If a user (programmer) does not provide a mechanism
to handle these anomalies, the .NET run time
environment provides a default mechanism that
terminates the program execution.
December 12, 2024 Prepared By : Mittal Rayjada 4
Exception Handling
 try
 A try block identifies a block of code for which particular
exceptions is activated.
 Catch
 A program catches an exception with an exception handler at
the place in a program where you want to handle the problem.
 finally
 The finally block is used to execute a given set of statements,
whether an exception is thrown or not thrown.
 throw
 A program throws an exception when a problem shows up.

December 12, 2024 Prepared By : Mittal Rayjada 5


Syntax
 You can list down
multiple catch
statements to catch
different type of
exceptions in case your
try block raises more
than one exception in
different situations.

December 12, 2024 Prepared By : Mittal Rayjada 6


Exception Classes in C#

 The exception classes in C# are mainly


directly or indirectly derived from the
System.Exception class.
 Some of the exception classes derived from
the System.Exception class are the
System.ApplicationException and
System.SystemException classes.

December 12, 2024 Prepared By : Mittal Rayjada 7


 The System.ApplicationException class
supports exceptions generated by application
programs.
 Hence the exceptions defined by the
programmers should derive from this class.
 The System.SystemException class is the
base class for all predefined system exception.

December 12, 2024 Prepared By : Mittal Rayjada 8


December 12, 2024 Prepared By : Mittal Rayjada 9
Handling Exceptions

 Exceptions can be handled using try and catch


blocks.
 With the use of these blocks core
programming statements can be distinguished
from error – handling statements.

December 12, 2024 Prepared By : Mittal Rayjada 10


using System;
namespace ExceptionHandlingDemo
{
class Program
{
static void Main(string[] args)
{
int num1, num2, result;
num1 = 12;
num2 = 0;
try
{
result = num1 / num2;

}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception caught: {0}", e);
Console.ReadKey();
}
finally
{

Console.WriteLine("Hello");
Console.ReadKey();
}

}
}
}
C# Delegates

 Similar to pointer in C or C++.


 A delegate is a reference type variable that
holds the reference to a method. The reference
can be changed at runtime.
 Delegates are especially used for
implementing events and the call-back
methods.
 All delegates are implicitly derived from the
System.Delegate class.
December 12, 2024 Prepared By : Mittal Rayjada 12
Declaring Delegates

 Delegate declaration determines the methods


that can be referenced by the delegate.
 A delegate can refer to a method, which has
the same signature as that of the delegate.
 E.g.
public delegate int MyDelegate (string s);
 delegate <return type> <delegate-name>
<parameter list> SYNTAX

December 12, 2024 Prepared By : Mittal Rayjada 13


 Delegates are invoked using new keyword.

December 12, 2024 Prepared By : Mittal Rayjada 14


C# - File I/O

 A fileis a collection of data stored in a disk


with a specific name and a directory path.
 When a file is opened for reading or writing, it
becomes a stream.
STREAM

INPUT STREAM OUTPUT STREAM

December 12, 2024 Prepared By : Mittal Rayjada 15


December 12, 2024 Prepared By : Mittal Rayjada 16
The FileStream Class

 The FileStream class in the System.IO


namespace helps in reading from, writing to and
closing files.
 Syntax
FileStream <object_name> = new
FileStream( <file_name>, <FileMode Enumerator>,
<FileAccess Enumerator>, <FileShare Enumerator>);
 Example
 FileStream F = new FileStream("sample.txt",
FileMode.Open, FileAccess.Read, FileShare.Read);
December 12, 2024 Prepared By : Mittal Rayjada 17
FileMode

 Append: It opens an existing file and puts cursor


at the end of file, or creates the file, if the file
does not exist.
 Create: It creates a new file.
 CreateNew: It specifies to the operating system,
that it should create a new file.
 Open: It opens an existing file.
 OpenOrCreate: It specifies to the operating
system that it should open a file if it exists,
otherwise it should create a new file.
December 12, 2024 Prepared By : Mittal Rayjada 18
December 12, 2024 Prepared By : Mittal Rayjada 19
C# - Reading from and Writing to
Text Files
 The StreamReader and StreamWriter classes
are used for reading from and writing data to text
files.
 These classes inherit from the abstract base class
Stream, which supports reading and writing bytes
into a file stream.
 The StreamReader Class
 The StreamReader class also inherits from the
abstract base class TextReader that represents a
reader for reading series of characters.
December 12, 2024 Prepared By : Mittal Rayjada 20
Methods of StreamReader Class

December 12, 2024 Prepared By : Mittal Rayjada 21


C# - Collections
 Collection classes are specialized classes for data
storage and retrieval.
 These classes provide support for stacks, queues,
lists, and hash tables.
 Collection classes serve various purposes, such as
allocating memory dynamically to elements and
accessing a list of items on the basis of an index etc.
 These classes create collections of objects of the
Object class, which is the base class for all data
types in C#
December 12, 2024 Prepared By : Mittal Rayjada 22
Array List

 It represents ordered collection of an object


that can be indexed individually.
 It is basically an alternative to an array.
 However, unlike array you can add and
remove items from a list at a specified
position using an index.
 Array resizes itself automatically.
 It also allows dynamic memory allocation,
adding, searching and sorting items in the list.
December 12, 2024 Prepared By : Mittal Rayjada 23
December 12, 2024 Prepared By : Mittal Rayjada 24
Hash Table

 Ituses a key to access the elements in the


collection.
 A hash table is used when you need to access
elements by using key, and you can identify a
useful key value.
 Each item in the hash table has a key/value
pair.
 The key is used to access the items in the
collection.
December 12, 2024 Prepared By : Mittal Rayjada 25
December 12, 2024 Prepared By : Mittal Rayjada 26
December 12, 2024 Prepared By : Mittal Rayjada 27
Sorted List
 The SortedList class represents a collection of key-
and-value pairs that are sorted by the keys and are
accessible by key and by index.
 A sorted list is a combination of an array and a hash
table.
 It contains a list of items that can be accessed using a
key or an index.
 If you access items using an index, it is an ArrayList,
and if you access items using a key, it is a Hashtable.
 The collection of items is always sorted by the key
value.
December 12, 2024 Prepared By : Mittal Rayjada 28
December 12, 2024 Prepared By : Mittal Rayjada 29
December 12, 2024 Prepared By : Mittal Rayjada 30
THANK YOU

December 12, 2024 Prepared By : Mittal Rayjada 31

You might also like