Friday, 28 June 2013

Why we used ref keyword ?

Suppose you have to get the value that has changed inside function. to get that changed value outside the function then use ref keyword.

1. This should be used when you re-instantiate the object inside function. 

2. This should be used, if your object is string or any value type Object.

Class1 ObjClass = new Class1();
ObjClass.StrA = 55;
GetA(ref ObjClass);
int ss = ObjClass.StrA; //ss value is 99 after re-instatiate inside function

private void GetA(Class1 ObjClass)
{
   ObjClass = new Class1();
   ObjClass.StrA = 99;
}

No comments:

Post a Comment