Differences between the ref and out keyword ?

  • Thread starter Thread starter Steve B.
  • Start date Start date
S

Steve B.

Hello

I'm wondering what is exactly the difference between "ref" and "out"
keywords.

Thanks,
Steve
 
thx :)

Tamir Khason said:
Ref parameter Should be initilized before call.
Out need not to initilized before call.

Ref paramenter is nothing but in & out parameter.
Out is only out parameter.

There is no performance issue occured.

A ref or out argument must be an lvalue.ie, you need to pass the
same
signature.The casting is not allowed for both when you pass the
values to ref/out.
ex.
public void Test(ref int a, out int b)
{
a=100;
b=200;
}

public void Caller()
{
int a,b;
a=10; //If you miss init it will give
error.
Test(ref a,out b);

object x=10,y;
Test(ref (int)x,out (int)y); // It is
not Allowed.Bcs it is not lValue.
}
 
Ref parameter Should be initilized before call.
Out need not to initilized before call.

Ref paramenter is nothing but in & out parameter.
Out is only out parameter.

There is no performance issue occured.

A ref or out argument must be an lvalue.ie, you need to pass the
same
signature.The casting is not allowed for both when you pass the
values to ref/out.
ex.
public void Test(ref int a, out int b)
{
a=100;
b=200;
}

public void Caller()
{
int a,b;
a=10; //If you miss init it will give
error.
Test(ref a,out b);

object x=10,y;
Test(ref (int)x,out (int)y); // It is
not Allowed.Bcs it is not lValue.
}
 
C Addison Ritchie said:
"ref" and "out" are very similar with the exception that the initial
value of the "out" parameter provided by the caller is unimportant.

Not only is it unimportant - it doesn't even have to be definitely
assigned. However, every normal exit of a method with an "out"
parameter *does* have to make sure it will be definitely assigned by
that point.
 
Back
Top