Friday, September 30, 2011

Working With VISUALSTUDIO .NET -


Working With VISUALSTUDIO .NET – it’s an editor used for developing .net application in any .net language like C#, VB etc. as well as any type of application like console, windows , web etc.

Versions of VISUAL STUDIO .NET –
YEAR
Version
2002
framework 1.0
2003
framework 1.1
2005
framework 2.0
2008
framework 3.5
2010
framework 4.0
  1. TO open VS go to start menu  programs ms visual studio  ms visual studio and click on it to open.
  2. Applications developed under visual studio are known as Projects” where a “project” is the collection of various files or items. To create a project either click on  new project option or go to file menu  select new project, which opens new project window.
  3. under new Project window we need to specify the following details

  •       In the LHS chose the language in which we want to develop the application eg Visual C# .
  •       In the middle chose the type of application we want to develop by choosing a project template. eg – console application.
  •      In the bottom specify a name to the project. eg – SampleProject.
  •      Below project name specify the location where to save the project. eg- C:\CSharp4

          4. click on OK button which creates the project with a default class program under the file Program.cs
          5. When projects are developed in VS by default a Namespace is gets created with same name of the        Project i.e. SampleProject. From now each and every class of the project comes within the same            name space.
NOTE - a Namespace is a logical continuer of types.
          6. Now under Main method of Program Class write the following code.
        Console.WriteLine(“My first Project”);
        Console.WriteLine();
         7. To run the class either presses F5 or ctrl + F5 or Click on start debugging button on top of the                 studio which will saved compile and executes the program.
         8. under VS we find a window known as “ Solution Explorer” used for organizing the complete                      application, which allows to view, add or Delete items under the project, to open ”solution"                      explorer  go to view menu and select “solution Explorer “
         9. to add new class under project open solution explorer  Right click on Project  select add new              item  which opens add new item window select class template in it specify a name in the                      bottom  click on add button which add the class under project eg- class1.cs
     Note – the new class added also comes under the same Namespace.
         10. Now under the class write following code.
static void Main()
{
Console.WriteLine(“second class”);
Console.ReadLine();
}
        11.To run the class open Solution Explorer  right click on the Project  select properties which                opens project property window  under it we find an option  startup objects which lists all the                classes of project that contains a valid main method in them  choose your class and run.

Objects Oriented Programming It is an approach that came into existence in 70th to resolve the drawbacks of traditional procedural approach i.e. security and reusability.
Object Oriented Languages provides Security and Reusability under them we can call a language as OO provided it satisfied the principals of the approach, those are –
  1. Encapsulation.
  2. Abstraction.
  3. Inheritance.
  4. Polymorphism.
As per Encapsulation the code (members) of an OOP has to be enclosed under a wrapper or continuer known as class that provides basic security for contain that is present inside it
Abstraction is all about hiding the complexity, behind the code by providing a set of interfaces used for consuming the functionality. Methods are the best example for abstraction because we never know what is the underling code in it, what we required to know only how to invoke it.

Inheritance - provides code reusability which can be used for consuming members of a class from other class by stabilizing parent/child relation between classes.
Polymorphism  is an approach which tells entities behaves in different ways depending upon the inputs it receives i.e. whenever the input changes the output and the behavior of the entity also changes, which can be implemented using approaches like Overloading and overwriting.

2D array


2D Array
Assigning values to 2D array while Declaration –
int[,]arr = { {11,12,13,14},
{21,22,23,24},
{31,32,33,34},
};
Jagged ArraysThese are also two dimensional that contains rows and columns whereas in two dimensional arrays all the rows will have same no of columns but in a jagged array each row will be having varying or different no of columns.

This are also referred as array of arrays because multiple single dimensional array are combined together to form a new array.
(type)[ ][ ] (name) = new (types)[rows][ ];
int[ ][ ] arr = new int[5][ ];
int [ ] arr = {list of values};
To declare a jagged array in the initial declaration we can only specified the no of rows to the array but not the columns because columns are not fixed.
int[ ][ ] arr = new int[4][ ];
After specifying the no of rows now pointing to each row we need to specify the no. of columns to the row.
int [ ] [ ] arr = new int[4][ ];
arr[0] = new int[5];
arr[1] = new int[6];
arr[2] = new int[8];
arr[3] = new int[4];

        Program
For Assigning Values to Jagged Array while Declaration –
int [ ][ ]arr = {
new int[3]{11,12,13},
new int[5]{21,22,23},
new int[4]{31,32,33},
new int[3]{41,42}
};
Command Line Parameters In the execution of a program it may required from values that needs to supplied in rum time where the values can be supplied in two diff ways –
  1. The program prompts for a value for which we supply the value and within the program the value is captured using reed line method.
  2. In the second case you can supply a list of values for any program at the of execution from the command prompt besides the program name, separating each value with a space.
    using System;
    class ParamsDemo
    {
    static void Main(string[] args)
    {
    foreach(string str in args)
    Console.WriteLine(str);
    }
    }
    For the above program while executing the program we can supply a list of values as command line parameters where all those values will be tacking into the string array of main method. To check the execute the above program as following.
    C:\csharp4>ParamsDemo 100 hello 3.14 true
    Program –
using System;
class AddParams
{
static void Main(string[] args)
{
int sum = 0;
foreach(string str in args)
sum += int.Parse(str);
Console.WriteLine(sum);
}
}

Tuesday, September 27, 2011

Arrays


Arraysit’s a set of similar type value that is store sequentially. They can be arranged in three different types like –
  1. single dimensional
  2. two dimensional
  3. Jagged arrays.
In C# arrays can be declared as fixed length or dynamic. Fixed length array can store a predefined number of items while the size of dynamic array increases as you add new items to the array.
Single Dimensional Array  These are array which store the values in the form of a row.
Syntax of defining the 1 D Array -
(Type) [] (name) = new (type) [size];
int [] arr = new int [4];
or
int [] arr ;
arr = new int[5];
or

int [] arr = {list of values};

Program
using System;
class SDArray
{
static void Main()
{
int[] arr = new int[5];
arr[0]=10;arr[1]=20;arr[2]=30;arr[3]=40;arr[4]=50;
for(int i=0;i<5;i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
foreach(int i in arr)
Console.Write(i + " ");
}
}
foreach loop As discussed earlier it has been specially designed for processing values of an array or collection for each iteration of the loop one value of the array is assigned to the variable of the loop and returned to us.
In case of a for loop the variable of the loop refers to index of the array whereas in case of the foreach loop it refers to values of the array.
In case of a for loop the loop variable will always be in only whereas in case of a foreach loop the loop variables type depends upon the type of values in the array.
Note – Arrays are reference type because they give us flexibility to declare a array without specifying any size and later the size can be specified, where this flexibility is given only to reference type.
Array Class- It is a predefined class under the system name space which provides a set of methods and properties (variables) which can be applied on an array.
  • sort (array)
  • reverse (array)
  • copy (src, dest , n)
  • length
Note- short, reverse, copy is static members. It is declared like as Arr.short etc..

Program
using System;
class SDArray2
{
static void Main()
{
int[] arr = {34, 78, 20, 98, 72, 48, 56, 61, 70, 8, 15};
for(int i=0;i
Console.Write(arr[i] + " ");
Console.WriteLine();
Array.Sort(arr);
foreach(int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Array.Reverse(arr);
foreach(int i in arr)
Console.Write(i + " ");
Console.WriteLine();
int[] brr = new int[10];
Array.Copy(arr, brr, 5);
foreach(int i in brr)
Console.Write(i + " ");
Console.WriteLine();
}
}
TWO Dimensional Arrays – These are the arrays which can store values in the form of rows and columns.
(type)[,](name) = new (type)[rows,cols];
int[,]arr = new int[3,4];
or
int[,]arr;
arr = new int[2,3];
or
int[,] = {list of values};

Program

Operators in C#



Arithmetic –
+, -, *, /, %
Assignment –
=, +=, -=, *=, /=, %=
Comparison –
++, !+, <, <=, >, >=, is, as, like.
Concatenation –
+
Increment and decrement –
++ , --
Logic –
&& , ||, ^
Conditional statement: - A block of code which gets executed basing on a condition is a conditional statement.
There are two types 
  • Conditional branching
  • Conditional looping.

  1. Conditional branching : this statement is also allow you to branch your code depending on whether certain condition were met or not. C# has two constructs for branching code, the if statement which allows you to test whether a specific condition is met and the switch statement which allows you to compare and expression with a no. of different values
    If ( < condition > )
    < stmts > ;
    else if ( < condition > )
    < style="mso-spacerun:yes"> >;
    ----------------------.
    else
    < stmts >;
    Program of IF Statement
    using System;
    class IfDemo
    {
    static void Main ()
    {
    int x, y;
    Console.Write("Enter x Value :");
    x = int.Parse(Console.ReadLine());
    Console.Write("Enter y Value :");
    y = int.Parse(Console.ReadLine());
    if (x>y)
    Console.WriteLine("x is greater");
    else if (x
    Console.WriteLine("y is greater");
    else
    Console.WriteLine("Both are equal");
    }
    }
    Switch case – the expression of switch case.
    switch ( < expression >)
    {
    Case < value > :
    < statement >;
    Break ;
    .-----------------.
    default :
    < stmts >;
    break;
    Using break after each case is mandatory in C# and also break has to be used after default
Program of Switch
using System;
class SwitchDemo
{
static void Main()
{
Console.Write("Enter a student no. (1-3): ");
int sno = int.Parse(Console.ReadLine());
switch (sno)
{
case 1:
Console.WriteLine("Student 1");
break;
case 2:
Console.WriteLine("Student 2");
break;
case 3:
Console.WriteLine("Student 3");
break;
default:
Console.WriteLine("Invalid student no");
break;
}
}
}
Condition loop –
C# provides 4 different loops that allows you to execute a block of code repeatedly until the certain condition is mat those are –
  1. for loop
  2. while loop
  3. do …. while loop
  4. foreach loop
Every loop required three things in common
  1. Initialization  which sets the starting point of loop.
  2. Condition  which sets the end of a loop.
  3. Iteration  this tacks you to the next level of the loop, either in forward or backward direction.
FOR LOOP 
for ( initialize ; condition , iteration)
< statement > ;
for ( int x =1 ; x <= 100; x++)
console.WriteLine (x);



While Loop
while ( condition)
< statement > ;
int x = 1;
while ( x <= 100)
}
Console.writeLine (x);
x++;
}

DO…… WHILE LOOP 
This is similar to while loop but for the first execution of the loop it doesn’t require any condition test. After completing the execution for one time then it checks for the condition to execute for next time.
do
{
< statement >;
while ( condition );
int x = 1 ;
do {
Console.WriteLine(x);
x++;
} while (x<= 100);

FOREACH LOOP  it is a special loop that has been design for the processing of arrays and collection.
Note- array is the set of similar type value, and the collection is the set of dissimilar type value.
foreach (type value in coll / array)
{
< statement>;
}
JUMP STATEMENT  C# provides a no. of statements that allow you to jump immediately to another line in a program w have four different jump statement support in C# those are –
  1. goto
  2. break
  3. continue
  4. return
GOTO  IT allows you to jump directly to another specified line in the program indicated by a label which is an identifier followed by a colon (;).
goto xxx;
console.WriteLine (“ Hello”);
xxx;
Console.WriteLine(“ goto called”);
BREAK  it is used to exit from a case in a switch statement and also used to exit from for, foreach, while and do…..while loop. Which will switch the control to the statement immediately after and of the loop.
for ( int i=1 ; i<= 100; i++)
{
Console.WriteLine (i);
if (i== 50)
break;
}
Console.WriteLine(“ End of the loop “);
CONTINUE  this can be used only in the loop statements which will jumped the control to iteration part without executing the statements present after it.
for ( int i=1 ; i<= 100; i++)
{
if (i=7)
continue;
Console.WriteLine (i);
}
Console.WriteLine(“ End of the loop “);
RETURN  If a jump statement which can come out of a method or function if required in the mid of it execution as well as it is also capable to carry a value from the method or function outside.
program using return -
using System;
class RetDemo
{
static void Main()
{
Console.Write("Enter a numeric value: ");
int no = int.Parse( Console.ReadLine() );
if ( no <>
return;
for(int i=1;i<=10;i++)
Console.WriteLine("{0} * {1} = {2}", no, i, no * i);
}
}