Saturday, 10 October 2015

ARRAYS IN c#:

c# treats multidimensional arrays as "arrays of arrays".Simplest form of multidimensional array is 2-dimensional array.
2-dimensional array is declared as:
int[ , ] Array_name=new int[rows,columns];        //rectangular array
eg:
   int[ , ] array=new int[3,3];

Two Dimensional Arrays in C#

second is variable size arrays that are knows as jagged arrays.
The jagged array is two dimensional array having different length for each row.It can be declared as:
              int[ ][ ] array_name=new [row][column];
eg:
           int[ ][ ] array=new int[3][ ];          //three rows array
           array[0]=new int[1];                  //first row has one element
           array[1]=new int[2];             //2nd row has two
           array[2]=new int[3];             //3rd row has three elements

FOREACH STATEMENT:

foreach statement is a new thing in c#.its quit similar to the for statement but implemented differently.The foreach statement repeats a group of embedded statements for each element in an array or an object collection.
for example:
program: 
/*array using foreach statement*/
using System
class Test
{
    public static void Main()
    {
          int[ ] array={1,2,3,4,5}
          foreach(int a in array)                 //foreach statement where  in is a keyword
          {
               Console.WriteLine(" "+m);
          }
     }
}

foreach statement automatically detect the boundaries of the collection being iterated. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block.
At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword. 

Friday, 25 September 2015

USE OF CASTING OPERATION:

Program p2: /*A simple program showing use of casting operation*/

using system;                            //namespace
class Casting
{
    public static void Main()
    {
          float sum;
          int i;
          sum=0.0F;              
          /*value initialized to a real variable must followed by F,otherwise it will be
            considered as decimal value*/
          for(i=1;i<=10;i++) 
          {
                 sum+=1/(float)i;                  //type casting int to float
                 Console.Write("i==>"+i);
                 Console.WriteLine("Sum==>"+sum);
          }
    }
}

So,
Casting into a smaller type may result in loss of data.
For example from above example if we try to convert the float type into int type,it may result in the loss of fractional part.

Thursday, 24 September 2015

BOXING AND UNBOXING

Boxing: 
           Boxing means conversion of a value type on the stack to a object type on the heap.When compiler finds a value type needs to convert in a reference type,it creates an object 'box'into that it places the value of value type.
Example:
                 int a=10;
                Object   b=a;                 //creates a box rm to hold m
                a=20;
Here boxing create a copy of  integer a to object b.Now both variables are different where the value of a resides on the stack and that of b on the stack.So,the values are independent of each other.
from above example value of a is 20 and that of b is 10.
Unboxing: 
               Its the process of converting the object type back to the value type.We can only unbox a variable that has previously been boxed.Its an explicit conversion because during unboxing the object can cast to any type.
Example:
              int a=10;
              object  b=a;                //boxing a implicitly
              int c=(int)b;                //unboxing explicitly
During unboxing,we have to ensure that the value type is large enough to hold the value of object.

DATA TYPES

Data types specify the size and the type of values that can be stored.
So,in C# there are two categories of data types:
(a) Value types
(b) Reference types
(a) Value types: 
                      These are stored on the Stack, and when value of a variable is assigned to another,they are actually copied.Hence two identical copies of that variable exits in memory.
hence it refers that two variables with same values in the memory.Value data types are: integers,real numbers, Boolean,characters,Enumerations,structures.
(b)Reference types: 
                             These are stored on heap, so when assignment of two reference variable occurs, only the reference is copied but actual value remains in d same memory location.Which implies that,there are two references to a single value.
reference data types are:objects,strings,classes,arrays,delegates,interfaces.



USING ALIASES FOR CLASS

Program P2:/*A program using aliases for class and command line arguments*/
using A=System.Console;                                     //A is alis for System.Console
class Sample
{
    public static void Main(string[ ] args)
    {
       A.WriteLine("welcometo");                           // Write can also be used in place of WriteLine
        A.WriteLine(" ",args[0]);                                  //command Line argument
        A.WriteLine(" ",args[1]);
    }
}

Command Line aruguments  are parameters suplied to the maine method at the time of invoking it for execution.args  that is given as the Main method parameter,is an array of strings. Arguments provided in the command line are passed to the arrayargs.
Also,C# is a platform free language, we need not to indent any line.

PROGRAM STRUCTURE

This is the program structure,where main section method is essential all other are optional.Where
using directive section includes includes all those namespaces that contains classes required in the application.It tells to the compiler to look for the class in that namespace.
Interface are used when we want to implement the concept of multiple inheritance ina a program.
class are included in class section.And last one is the main section method,Main method is the starting point in a c# program and so is essential part.There can be multiple Main methods in a C# program.
EXECUTING THE PROGRAM: 
             After creating a c program,save it with .cs file extension. Ex: sample.cs
And for compiling it simply type : csc sample.cs