Saturday, August 13, 2016

Exception handling in C#



An exception is the disruption that rises during the execution of program such as divide by zero. In C# provide the following keywords for exception handling: try, catch, finally and throw.

try – try block identifies the statement that throw and exception.

catch – the catch block handle the exception it any exists.

finally – the finally block is used to execute a set of statement whether the exception is thrown or not.

throw – The throw keyword is used to throw an exception programmatically.

The example of the exception handing is given below: - 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication20
{
    class program
    {
        public static void Main()
        {
            int x = 0;
            int y = 0;
            try
            {
                y = 100 / x;
                Console.WriteLine("Line is not executed");
            }
            catch (DivideByZeroException de)
            {
                Console.WriteLine("Exception rises");
            }
            finally
            {
                Console.WriteLine("Finally Block");
            }
            Console.WriteLine("Result is {0}", y);
            Console.ReadKey();
        }
       
    }
}
Output -  


                                                                Author - Sachin Pathak

Wednesday, August 10, 2016

File I/O in C#



File is the collection of data that is stored in a disk with specific name, when we open a file for read or write the data it becomes a steam.

The steam is the sequence of the byte passing through the communication path, there are two types of steam input steam and output steam.

Input steam – Input steam is used to read the data from the file.
Output steam – Output steam is used to write the data into file.

C# I/O Classes – In C# system.IO name space is used to performing operation with file.System.IO name space contains following classes.

BinaryReader – Used to read the primitive data from binary steam
Binarywriter – Used to write the primitive data in binary format.
Directory – Used to manipulating a directory structurer.
DirectoryInfo – Used to perform operation on directories.
DriveInfo – It gives the information for the drives.
File – Used for manipulating files.
FileInfo – It is used to perform operation on file.
Filesteam – Used to read and write from the file.
Path – Perform operation on path information.
SteamReader – It is used to read character from a byte steam.
Steamwriter – It is used to writing character to a steam.
StringReader – Used to read from string buffer.
StringWriter – Used to write into a string buffer.

Example – the following program is used for Filesteam class - 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication19
{
    class Program
    {
        FileStream FS = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

         for (int i = 1; i<= 20; i++)
         {
            FS.WriteByte((byte)i);
         }
        
         FS.Position = 0;
         for (int i = 0; i <= 20; i++)
         {
            Console.Write(F.ReadByte() + " ");
         }
         FS.Close();
         Console.ReadKey();
      }
}
Output - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1

                                                             Author - Sachin Pathak

Tuesday, August 9, 2016

What is array in C#



An array store fixed-size sequential collection of elements of the same datatype. It is used to store the collection of data of same data type at contiguous memory locations.

Declaration of array –
Datatype [ ] arrayname;

Datatype specify the data of elements in the array.
[ ] specify the size of the array.
And arrayname is the name of array.

Initializing an Array -
An array is the reference type so we need new keyword to create the instance of the array. For example –
double d= new double [10];

Assigning value to an Array –
We can assign the value to an array by using the index number. For example –
double d= new double [10];
d[0]=100;

we can also assign the value to an array at the time of declaration –
double[ ] d={10,15,20,25,30}

We can also create and initialize the array as the following ways
int[ ] mark=new int[5] {20,21,22,23,29}
or
int[] mark=new int[ ] {22,24,26,28,29}

The Example of the array is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] n = new int[10];
            int i, j;
            for (i = 0; i < 10; i++)
            {
                n[i] = i + 10;
            }
            for (j = 0; j < 10; j++)
            {
                Console.WriteLine("index[{0}] = {1}", j, n[j]);
            }
            Console.ReadKey();
        }
    }
}
 
Output - 
Following are the some important concept related to the array.

Multi-dimensional arrays – C# support multi-dimensional arrays. It is also called two dimensional arrays.

Jagged arrays – The array of array is also jagged arrays.

Passing arrays to functions – We can pass a function to an array by specifying the array name without an index.

Param arrays – Param arrays is used to pass unknown number of parameter to a function

The array class – The array class provides the various properties and methods by define system name space.