C Sharp StreamReader

C# StreamReader To read a string from the stream, the C# StreamReader class is used which inherits the TextReader class. To read data from the stream, the Read() and ReadLine() methods are facilitated by the C# StreamReader class. Example: using System; using System.IO; public class Example { public static void Main(string[] args) { FileStream fs … Read more

Categories C#

C Sharp Checked and Unchecked

C# Checked and Unchecked To handle integral type exceptions in C#, the checked and unchecked keywords are used. They thus define the checked context where arithmetic overflow raises an exception and the unchecked context where the arithmetic overflow is ignored and the result is truncated. C# Checked: To check the overflow explicitly, the checked keyword … Read more

Categories C#

C Sharp StreamWriter

C# StreamWriter To write characters to a stream in a specific encoding, the C# StreamWriter class is used which inherits the TextWriter class. To write data into a file, the overloaded write() and writeln() methods are facilitated by the C# StreamWriter class. Example: using System; using System.IO; public class Example { public static void Main(string[] … Read more

Categories C#

C Sharp SystemException class

C# SystemException class C# provides a predefined exception class to handle the system-related exceptions. This exception class is named as the SystemException class to work as a base class for the system exception namespace. The ValidationException, ArgumentException, ArithmeticException, DataException, StackOverflowException, and many more exceptions. are the various child classes of the SystemException class. Various constructors, … Read more

Categories C#

C Sharp FileStream

C# FileStream To provide a stream for the file operation for performing the synchronous and asynchronous read and write operations, the FileStream class is used in C#. Example: writing a single byte into a file: using System; using System.IO; public class Example { public static void Main(string[] args) { FileStream fs = new FileStream("d:\\file.txt", FileMode.OpenOrCreate); … Read more

Categories C#

C Sharp User-Defined Exceptions

C# User-Defined Exceptions A user-defined or custom exception can be created in C# with a purpose to make a meaningful exception. The Exception class needs to be inherited to create a user-defined exception in C#. Example: using System; public class InvalidMarksException : Exception { public InvalidMarksException(String msg) : base(msg) {   } } public class … Read more

Categories C#

C Sharp finally

C# finally Whether the exception is handled or not, we need to execute the important code. To serve this purpose in C#, the finally block is used after the catch or try block. Example: When the exception is handled: using System; public class Example { public static void Main(string[] args) { try { int X … Read more

Categories C#

C Sharp try/catch

C# try/catch The try/catch statement is used in C# to perform exception handling. To place the code that may throw an exception, the try block is used in C# and to handle the exception, the catch block is used in C#. The try block must be placed before the catch block in C#. Example: Without … Read more

Categories C#

C Sharp Exception Handling

C# Exception Handling To handle runtime errors, the process of Exception Handling is used in C#. Even after the runtime errors, the normal flow of the application can be maintained using the exception handling. Exceptions in C# are events or objects thrown at runtime and are derived from the System.Exception class. It can be handled, … Read more

Categories C#

C Sharp Strings

C# Strings To represent a sequence of characters, strings are used in C#. It is an object of System.String class. Several operations like concatenation, comparison, getting substring, search, trim, replacement etc, can be performed on C# strings. Comparison between string and String: The string is an alias for System.String class. It is a keyword in … Read more

Categories C#