Thursday, 29 March 2018

FILE READING AND WRITING?
   Reading from and Writing to Text Files.

FILESTREAM CLASS:
 FileStream class used to read and write operating system files. FileStream can be used to perform the basic operations of reading and writing operating system files.

What is Encoding?
 convert (information or an instruction) into a particular form.

PROGRAM:(fileStreamEg.cs)
using System;
using System.IO;
using System.Text;
namespace fileStreamEg
{
class Program
{
static void Main(string []args)
{
try
{
FileStream fs=new                                       FileStream(@"D:\sample.txt",FileMode.Create,FileAccess.ReadWrite);
byte[] b=Encoding.ASCII.GetBytes("Hello World");//encoding the string and                                                                                                                            store in a byte array
for(int i=0;i<b.Length;i++)
{
fs.WriteByte(b[i]);  //Write a data in to the file 
}
Console.WriteLine("Current Position:"+fs.Position.ToString());
fs.Seek(0,SeekOrigin.Begin);
int data;
while((data=fs.ReadByte())>-1)  // read bytes until read all 
{
Console.WriteLine((char)data);
                                 }
fs.Close();
Console.WriteLine("File is created");//for user identification
}
catch(Exception e){Console.Write(e.Message);}
}
}
}

OUTPUT:
CONSOLE WINDOW OUTPUT:
D:\csharp>fileStreamEg
Current Position:11
H
e
l
l
o

W
o
r
l
d

File is created

FILE:(sample.txt)
Hello World


PROGRAM EXPLANATION:


The above program explain the read and write the file using filestream class,the file stream class used to perform the basic operation in files, Encoding.ASCII.GetBytes() that function encoding a given text to encoding format,and then the data write on a file and then read.

Wednesday, 28 March 2018

CUSTOM EXCEPTION

WHAT IS CUSTOM EXCEPTION ?

          Custom exceptions can be used to add meaningful, and user-friendly information  to exceptions when errors occur while your program is running.
Exception Class is a base class of the all exceptions.

PROGRAM:(CustomExceptionEg.cs)

using System;
using System.Collections;
namespace Trainning
{
class CustomExceptionEg:Exception           //Inherit the Exception class  for create the custom                                                                                                 exception
{
public CustomExceptionEg(string Message):base(Message)
{}

}
class program
{
public static void Main(string []args)
{
try
{
int age=24;
if (age>18)
throw new CustomExceptionEg("age is invalid"); 
else
Console.WriteLine("u are welcome");

}
catch(Exception e)
{
Console.WriteLine(e.Message);
}

}
}

}

OUTPUT:

D:\csharp>CustomExceptionEg

age is invalid

PROGRAM EXPLANATION:
   The above Program is a simple example for  Custom Exception,And the program condition is check the given age value is greater than 18 the program run regularly other wise it  throw the exception .






Exception Handling(Index was outside the bounds of the array)

Exception Handling:
        Exception handling is the process of responding to the any Exception during computation, or exceptional conditions requiring special processing – often changing the normal flow of program execution.
    
PROGRAM:(ExceptionEgSingleCatch.cs)

using System;
namespace training
{
class ExceptionEgSingleCatch
{
public static void Main(string []args)
{
try
{
int[] arr=new int[2];//Fixed the size of Array
arr[3]=4; //Storing data out of the index but it is throw exception
}
catch(Exception e) //catch block execute  when try block throws the error
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Rest of the code");//Finally block execute after the execution of try or catch block
}
}
}
}




OUTPUT:

D:\csharp>ExceptionEgSingleCatch
Index was outside the bounds of the array.
Rest of the code

PROGRAM EXPLANATION:
      The above program explain the exception handling and it take the index was outside the bounds of array,without exception handling concept it throw the runtime error but now it shows only the one line for that error,And before using the Exception handling the output was like following way.

PROGRAM WITHOUT USING EXCEPTION HANDLING CONCEPT:
using System;
namespace training
{
class ExceptionEgSingleCatch
{
public static void Main(string []args)
{
int[] arr=new int[2];//Fixed the size of Array
arr[3]=4;


}
}
}

OUTPUT:

D:\csharp>ExceptionEgSingleCatch

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at training.ExceptionEgSingleCatch.Main(String[] args)



Why  Exception Handling?
    We see above two examples ,the above examples are doing one operation (inserting data to the array),in that case poorly usage of exception handling but that the real time applications contains more number of operations that time this concept is very useful and that time really understand the features of Exception Handling. 

Tuesday, 27 March 2018

Array in c#

Array:
        
         An array stores a  collection of elements of the same type. An array is used to store a collection of data and it size is fixed-size.

PROGRAM: (ArrayEg.cs)

using System;
class ArrayEg
{
public static void Main()
{
int []array1={12,23,34,45,56,67}; //without using new keyword
int []array2=new []{11,22,33,44}; //without using datatype
int []array3=new int[]{0,1,2,3}; //using new and datatype
int[] array4=new int[5]; //get elemets in runtime
 Console.WriteLine("\n\n Array element in Array1");
foreach (int i in array1)
Console.WriteLine(i);
                Console.WriteLine("\n\n Array element in Array2");
foreach (int i in array2)
Console.WriteLine(i);
                Console.WriteLine("\n\n Array element in Array3");
foreach (int i in array3)
Console.WriteLine(i);
Console.WriteLine("Enter the array elements");
for (int j=0;j<5;j++)
{
array4[j]=int.Parse(Console.ReadLine()); //getting by user
}
 Console.WriteLine("\n\n Array element in Array4");
foreach (int i in array4)
Console.WriteLine("Array elements:\t"+i);

}
}


OUTPUT:

D:\csharp>ArrayEg
 Array element in Array1
12
23
34
45
56
67

 Array element in Array2
11
22
33
44

 Array element in Array3
0
1
2
3
Enter the array elements
4
3
2
1
0

 Array element in Array4
Array elements: 4
Array elements: 3
Array elements: 2
Array elements: 1

Array elements: 0

PROGRAM EXPLANATION:
           The above program explian about the array declaration,initialization and input to the array in run-time.

Thursday, 22 March 2018

Arithmetic Operations Using C#

Arithmetic Operators:
    The arithmetic Operators(+,-,*,/) are operates the two operants.
it processing the two operants and produce result of the operation .

PROGRAM:(ArithmeticEg.cs)
using System;
class ArithmeticEg
{
void addFunction(int a,int b)
{
double result=a+b; //Adding two integer values
Console.WriteLine("Sum of two values is:"+result);
}
void subFunction(float c,float d)
{
float result=c-d; //Subtraction of two integer values
Console.WriteLine("Subtraction of two values is:"+result);
}
void divisionFunction(double e,double f)
{
double result=e/f; //Division of two integer values
Console.WriteLine("Division of two values is:"+result);
}
void moduloFunction(int g,int h)
{
int result=g%h; //Modulo division of two integer values
Console.WriteLine("Modulo division of two values is:"+result);
}
void multiplicationFunction(int i,int p)
{
int result=i*p; //multiplication of two integer values
Console.WriteLine("Multiplication of two values is:"+result);
}


public static void Main(string[] args)
{
ArithmeticEg ip=new  ArithmeticEg(); //object creation

ip.addFunction(10,20); // Function calling
ip.subFunction(12.0f,3.9f);
ip.divisionFunction(12,1.1);
ip.multiplicationFunction(12,4);
ip.moduloFunction(13,2);




}

}

OUTPUT:

D:\csharp> ArithmeticEg
Sum of two values is:30
Subtraction of two values is:8.1
Division of two values is:10.9090909090909
Multiplication of two values is:48

Modulo division of two values is:1

PROGRAM EXPLANATION:
         The above program performing the arithmetic operation using functions.
it performs the addition,subtraction,Division,Multiplication and Modulo Division.



Saturday, 17 March 2018

c# Program for SUM of Two Numbers

PROGRAM:(AdditionEx.cs)

using System;
namespace Trainning
{
class AdditionEx
{
static void Main()
{
int a,b,sum; //you can declare any data type like float,double...
Console.WriteLine("Enter the Two values");
a=Convert.ToInt32(Console.ReadLine()); //input from keyboard
b=Convert.ToInt32(Console.ReadLine());
sum=a+b;
Console.WriteLine("Sum of Two Values"+sum);     //To print the text with value                                                                                                                       //stored  in  variable sum
}

}
}

OUTPUT:


Program Explanation:
       Above program for adding two numbers and input from the user

Wednesday, 14 March 2018

Hello World Program in C#(Using Text Editor)


Program:(HelloWorldProgExample.cs)

using System;
namespace Train
{
class HelloWorldProgExample
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}


output:
Program Explanation:
           
    Above program print the "Hello World!" on console window.