By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

Differences between the ref and out keyword ?

P: n/a
Hello

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

Thanks,
Steve
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
}
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Steve B." <st***************@airsoftconsulting.com> wrote in message
news:41**********************@news.free.fr...
Hello

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

Thanks,
Steve

Nov 16 '05 #2

P: n/a
thx :)

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> a écrit dans le message de
news:eo**************@tk2msftngp13.phx.gbl...
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.
}
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Steve B." <st***************@airsoftconsulting.com> wrote in message
news:41**********************@news.free.fr...
Hello

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

Thanks,
Steve


Nov 16 '05 #3

P: n/a
Hi Steve:

See Jon's article:

Parameter passing in C#
http://www.yoda.arachsys.com/csharp/parameters.html

--
Scott
http://www.OdeToCode.com

On Thu, 29 Jul 2004 15:33:52 +0200, "Steve B."
<st***************@airsoftconsulting.com> wrote:
Hello

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

Thanks,
Steve


Nov 16 '05 #4

P: n/a
C Addison Ritchie <CA*************@discussions.microsoft.com> wrote:
"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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.