Tuesday, 3 January 2012

Use of ref and out parameter


1) Use of Ref and Out parameter :


Ref -is used for pass by reference rather than pass by value ..
        pass by reference require initialization of variable .
Class Addittion
{
        public int Add(ref int a , ref int b)
{
        return a+b;
}

}


Class Mainclass
{
public static void Main()
{
    int a =10;
    int b =20 ;
Addittion additem =new Addition();


 int result =additem.Add(ref a ,ref b);  //it will just pass reference not value ..


//     result =30


}
}
------------------------------------------------------------------------------------------


Out - initialization is not required for out parameter .
but assignment is necessary before return call. 
it is use to pass value out of method  .





Class Addittion
{
        public void Add( int a ,  int b out int ans)
{
        ans = a+b;
}

}


Class Mainclass
{
public static void Main()
{
     int a =10;
    int b =20 ;
int result ;
Addittion additem =new Addition();


  additem.Add( a , b ,out result  );  //it will just pass reference not value ..


//     result =30


}
}

No comments:

Post a Comment