Monday, 16 April 2018

DELEGATES

What is Delegate?
delegate  is similar to a function pointer in C or C++. To call the reference method using  the delegate object.The delegates typedefinition same as the Function typedefinition.


PROGRAM:(DelegateEg.cs)

using System;
delegate int NumberChanger(int n);
namespace Training
{
class DelegateEg
{
static int num=10;
public static int AddNum(int  p)
{
num+=p;
return num;
}
public static int MulNum(int q)
{
num*=q;
return num;

}
public static int getNum()

{
return num;
}
public static void Main(string []arg)
{
NumberChanger nc1=new NumberChanger(AddNum);
NumberChanger nc2=new NumberChanger(MulNum);
nc1(25);
Console.WriteLine("Value of Num:{0}",getNum());
nc2(5);
Console.WriteLine("Value of Num:{0}",getNum());

}
}
}

OUTPUT:

D:\csharp>DelegateEg
Value of Num:35

Value of Num:175


PROGRAM EXPLANATION:
        The above program explain how to use the delegates in the c#.The delegate canbe declared outside the class and to create a object to the delegate and call the function using delegate objects.

Monday, 2 April 2018

Sorted List(GENERIC COLLECTIONS)

COLLECTION:
    A collection is a group of multiple objects. You can group any type of object in a collection into a single collection using a System.Object class. For that the .Net Framework provides collection classes, some in the System.Collections namespace and some in the System.Collecctions.Generic namespace.

GENERIC COLLECTION:
       The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide  the better type safety and performance than non-generic strongly typed collections.
SORTED LIST:
   SORTED LIST is a list it contains a keyvaluepairs and key is unique.

PROGRAM:(GenericCollectionEg.cs)

using System;
using System.Collections.Generic;
using System.Linq;
class GenericCollectionEg
{

public static void Main()
{
SortedList<int,String> slist=new SortedList<int,String>(); //Object Initialization
slist.Add(1,"cat");          //adding key value pair in the sorted list
slist.Add(4,"rat");
slist.Add(3,"bat");
for(int i=0;i<slist.Count;i++)      //Access the sortedlist keyvalues using for loop
Console.WriteLine("key:{0}\t,value:{1}",slist.Keys[i],slist.Values[i]);
foreach(KeyValuePair<int,String> kvp in slist) //using foreach loop
{
Console.WriteLine("key:{0},value:{1}",kvp.Key,kvp.Value);
}
var data=slist[3];  //to assigning a  value to the data (local variable) using key
Console.WriteLine(data);

KeyValuePair<int,String> result=slist.Where(g=>g.Key==4).FirstOrDefault(); // using linq
Console.WriteLine(result.Key+""+result.Value);



var query=from kvp in slist
  where kvp.Key==1
  select kvp;
var res=query.FirstOrDefault();
Console.WriteLine("Key:{0},Values:{1}",res.Key,res.Value);


var re=(from kvp in slist
where kvp.Key==3
select kvp).FirstOrDefault();
Console.WriteLine("Key:{0},Values:{1}",re.Key,re.Value);
        }
    }

OUTPUT:

D:\csharp>GenricCollectionEg
key:1   ,value:cat
key:3   ,value:bat
key:4   ,value:rat
key:1,value:cat
key:3,value:bat
key:4,value:rat
bat
4rat
Key:1,Values:cat
Key:3,Values:bat

PROGRAM EXPLANATION:
     The above program explain the sorted list,the sorted similar to the dictionary but it sorted based on the key value automatically.And then the sorted list storing the data in the list .The sortedlist under the genericcolections.

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.