Sunday, July 22, 2007

Ref keyword: Basics, Code Listing

Ref:
Ref is used to pass parameters by reference
Any changes made to the parameter in the method will be reflected in the variable
ref parameter must be initialized before its use
Out:
Out is used to pass parameters by reference
Parameter not required to be initialized before use
Any changes made to the parameter in the method will be reflected in the variable

CodeListing:Ref

class RefExample
{
static void Method(ref int i)
{
i=44;
}

static voic Main()
{
int val=0;
Method(ref val) // val is now 44
}

}

No comments: