Namespace,c# Exception , File Stream.

NameSpace : -

  • NamespacesNamespaces in C# are used to organize too many classes so that it can be easy to handle the application.
  • In a simple C# program, we use System. Console where System is the namespace and Console is the class. 
  • To access the class of a namespace, we need to use that namespace name in our class program.
  • We can use using keyword so that we don't have to use complete name all the time.
  • There are different namespace in c#. 
  1. using system.net;
  2. using system
  3. using system.Data;
  4. using system.Data.SqlClient;

C# Exception : -

  • Exception Handling in C# is a process to handle runtime errors. We perform exception handling so that normal flow of the application can be maintained even after runtime errors.



C# Exception Handling Keywords

In C#, we use 4 keywords to perform exception handling:
  • try
  • catch
  • finally, and
  • throw
    1. Try-catch: -
      1. In C# programming, exception handling is performed by try/catch statement. The try block in C# is used to place the code that may throw the exception. The catch block is used to handle the exception.
      2.  The catch block must be preceded by try block
    2.  finally : -
      1. C# finally block is used to execute important code which is to be executed whether the exception is handled or not. It must be preceded by catch or try block.
    3. throw : -
      1. it throws an error. throw may have classes related to the error.











C# FileStream

  • C# FileStream class provides a stream for file operation. It can be used to perform synchronous and asynchronous read and write operations. By the help of FileStream class, we can easily read and write data into the file.






C# FileStream example: writing multiple bytes into the file

Let's see another example to write multiple bytes of data into the file using the loop.




C# FileStream example: reading all bytes from file

Let's see the example of FileStream class to read data from the file. Here, ReadByte() method of FileStream class returns single byte. To all read all the bytes, you need to use loop.



C# StreamWriter

C# StreamWriter class is used to write characters to a stream in specific encoding. It inherits TextWriter class. It provides overloaded write() and writeln() methods to write data into file.


C# StreamReader

C# StreamReader class is used to read string from the stream. It inherits TextReader class. It provides Read() and ReadLine() methods to read data from the stream.

C# StreamReader example to read one line



C# StreamReader example to read all lines


C# TextWriter

C# TextWriter class is an abstract class. It is used to write text or sequential series of characters into file. It is found in System.IO namespace.



C# TextReader

C# TextReader class is found in System.IO namespace. It represents a reader that can be used to read text or sequential series of characters.

C# TextReader Example: Read All Data



C# TextReader Example: Read One Line




Comments

Popular posts from this blog

Asp.Net Validation

Fundamental of OOPS From Scratch to End