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

Passing Arguments ByRef/ByVal

P: n/a
Does C# support passing arguments ByVal & ByRef? If so, what is the default
(ByVal?)? And, if so, how do you explicitly specify either?

Thanks.
Sep 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Scott M." <No****@NoSpam.comwrote in message
news:OA**************@TK2MSFTNGP04.phx.gbl...
Does C# support passing arguments ByVal & ByRef?
Yes.
If so, what is the default (ByVal?)?
Yes
And, if so, how do you explicitly specify either?
int x = 1;
DoSomething(ref x);

void DoSomething(ref int x)
{
}

or

int x;
DoSomething(out x);

void DoSomething(out int x)
{
}
>
Thanks.

Sep 21 '06 #2

P: n/a

Scott M. wrote:
Does C# support passing arguments ByVal & ByRef? If so, what is the default
(ByVal?)? And, if so, how do you explicitly specify either?

Thanks.
As, Michael C pointed out - the answer is yes. The default, as in
VB.NET is by value (ByVal), but it is not specified explicitly.
Michael showed you that there are, in fact, two ways of passing by
reference (ByRef) in C# - but didn't clearly explain the difference. I
thought I might point that out...

The first way is via the ref keyword. It looks like this:

void SomeMethod (ref int theValue)

That says to pass, theValue by reference. But, the compiler will
require you to initialize the value before it is passed. So, when you
call the SomeMethod, it will look like:

int i = 10;
SomeMethod (ref i);

You can really think of ref as an In/Out parameter. Since, you can use
it to pass a value int and get a value back.

The second method, was via the out keyword. The out keyword does not
require the caller to initialize the value of the parameter. It is
required that the callee provide the value. If it doesn't then the
method will not compile:

void SomeMethod (out int theValue)

In this case, the usage would looke like:

int i;
SomeMethod (out i);

And again, SomeMethod would be required by the compiler to assign a
value to i. This is an Out parameter.

Anway - just thought I'd add a little bit more info to Michael's
excellent answer.

--
Tom Shelton

Sep 21 '06 #3

P: n/a
Scott M. <No****@NoSpam.comwrote:
Does C# support passing arguments ByVal & ByRef? If so, what is the default
(ByVal?)? And, if so, how do you explicitly specify either?
See http://www.pobox.com/~skeet/csharp/parameters.html

(There's no way of explicitly specifying ByVal - and it is indeed the
default.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #4

P: n/a
Thanks everyone. I was aware of ref and out and their requirements on having
a value set or not, but I was not aware that they caused the value to be
sent ByRef.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Scott M. <No****@NoSpam.comwrote:
>Does C# support passing arguments ByVal & ByRef? If so, what is the
default
(ByVal?)? And, if so, how do you explicitly specify either?

See http://www.pobox.com/~skeet/csharp/parameters.html

(There's no way of explicitly specifying ByVal - and it is indeed the
default.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.