C# Programming - (Advanced Concepts)
C# Programming - (Advanced Concepts)
Sealed Classes in C#
▪ You could restrict users form inheriting the class by sealing the class using the sealed keyword.
▪ The sealed keyword tells the compiler that the class is sealed, and therefore, cannot be extended.
▪ A method can also be sealed and in that case the method cannot be overridden.
▪ The exception will cause the abnormal termination of the program execution.
▪ Exceptions can be generated by the common language runtime (CLR), by the .NET Framework or any
third-party libraries, or by application code.
▪ Exception handling uses the try, catch, and finally keywords to try actions that may not succeed, to
handle failures when you decide that it is reasonable to do so, and to clean up resources afterward.
▪ Exceptions can be generated by the common language runtime (CLR), by the .NET Framework or any
third-party libraries, or by application code.
▪ Use a try block around the statements that might throw exceptions.
▪ Once an exception occurs in the try block, the flow of control jumps to the first associated exception
handler that is present anywhere in the call stack. In C#, the catch keyword is used to define an
exception handler.
▪ If no exception handler for a given exception is present, the program stops executing with an error
message.
▪ Exception objects contain detailed information about the error, such as the state of the call stack and
a text description of the error.
▪ Code in a finally block is executed even if an exception is thrown. Use a finally block to release
resources, for example to close any streams or files that were opened in the try block.
▪ ArithmeticException: A base class for exceptions that occur during arithmetic operations, such as
DivideByZeroException and OverflowException.
▪ ArrayTypeMismatchException: Thrown when an array cannot store a given element because the
actual type of the element is incompatible with the actual type of the array.
▪ NullReferenceException: Thrown when an attempt is made to reference an object whose value is null.
▪ OutOfMemoryException: Thrown when an attempt to allocate memory using the new operator fails.
This indicates that the memory available to the common language runtime has been exhausted.
▪ finally block:
▪ The purpose of a finally statement is to ensure that the necessary cleanup of objects, usually objects
that are holding external resources, occurs immediately, even if an exception is thrown.
▪ throw keyword:
▪ The throw is a keyword and it is useful to throw an exception manually during the execution of the
program and we can handle those thrown exceptions using try-catch blocks based on our
requirements.
▪ The throw keyword will raise only the exceptions that are derived from the Exception base class.
▪ The try block governs statements that are enclosed within it and defines the scope of the exception-
handlers associated with it.
▪ A try block must have at least one catch block.
▪ However, if none of the predefined exceptions meets your needs, you can create your own exception
classes by deriving from the Exception class.
▪ System exception: An exception that is raised implicitly under a program by the exception manager.
▪ Application exception: An exception that is raised explicitly under a program based on our own
condition (i.e. user-defined condition) is known as an application exception.
▪ The two basic streams used are the input and output streams.
▪ An input stream is used for a read operation and an output stream is used for performing a write
operation.
▪ The System.IO namespace includes various classes, which are used to perform operations, such as file
creation, file deletion, and the read-write operations to files.
▪ To open an existing file or to create a new file, you need to create an object of type FileStream.
▪ Consider the following syntax for creating the object of type FileStream:
FileStream <object name> = new FileStream(<file Name>,<FileMode Enumerator>,<File Access
Enumerator>,<FileShare Enumerator>);
▪ The FileMode enumerator parameter is specified in many constructors for the FileStream.
▪ FileAccess enumerator indicates whether you want to read data from the file, write to the file, or
perform both the operations.
▪ The members of the FileAccess enumerator are Read, ReadWrite, and Write.
▪ The FileShare enumerator contains constants for controlling the kind of access that the other
FileStream constructors can have to the same file.
▪ A typical use of this enumeration is to define whether two different applications can simultaneously
read from the same file.
▪ It is an abstract class, which supports reading and writing bytes into it.
▪ If data of a file is only text, then you can use the StreamReader class and the StreamWriter class
to accomplish the reading and writing tasks, respectively.
▪ The TextReader class represents a reader which can read a series of characters.
Methods Description
Close Closes the object of StreamReader class and the underlying stream, and releases
any system resources associated with the reader
Peek Returns the next available character but does not consume it
Read Reads the next character or the next set of characters from the stream
ReadLine Reads a line of characters from the current stream and returns data as a string
Seek Allows the read/write position to be moved to any position within the file
▪ The TextWriter class represents a writer, which can write a series of characters.
▪ The following table describes some of the commonly used methods of the StreamWriter class.
Methods Description
▪ Using classes such as the DirectoryInfo and FileInfo classes in combination is an efficient way
to gather the required information about files and directories in a specific location.
Property Description
Gets or sets attributes associated with the current file. This property is
Attributes
inherited from FileSystemInfo.
Gets or sets CreationTime of the current file. This property is inherited from
CreationTime
FileSystemInfo.
Exists Gets a Boolean value indicating whether the directory exist or not.
Gets a string containing the file extension. This property is inherited from
Extension
FileSystemInfo.
Gets a string containing the full path of the directory. This property is
FullName
inherited from FileSystemInfo.
Gets the last accessed time of the directory. This property is inherited from
LastAccessTime
FileSystemInfo.
Name Gets a string containing the name of a given file.
©Larsen & Toubro Infotech Ltd. Privileged and Confidential 20
Implementing the Windows File System
▪ The following table describes some of the commonly used methods of the DirectoryInfo class.
Method Description
Create Creates a directory.
CreateSubdirectory Creates a subdirectory.
Delete Deletes a directory.
Returns the directories in the current directory after matching
GetDirectories all the criteria. It also allows you to search subdirectories within
directories.
GetFiles Returns the files in the current directory.
▪ The following table describes some of the commonly used methods of the FileInfo class.
Method Description
Create Creates a file
AppendText Appends a text to the file represented by the FileInfo object
Delete Deletes a file
Open Opens file
OpenRead Opens a file in read-only mode
▪ Generic classes and methods combine reusability, type safety, and efficiency in a way that their non-
generic counterparts cannot.
▪ Generics are most frequently used with collections and the methods that operate on them.
▪ You can create your own generic interfaces, classes, methods, events, and delegates.
▪ Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the
application change.
▪ A collection is a class, so you must declare an instance of the class before you can add elements to that
collection.
▪ If your collection contains elements of only one data type, you can use one of the classes in
the System.Collections.Generic namespace.
▪ A generic collection enforces type safety so that no other data type can be added to it.
▪ IEnumerable:
▪ IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an
IEnumerator interface.
▪ This allows readonly access to a collection then a collection that implements IEnumerable can be
used with a for-each statement.