473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

please help me understand ref and out

Typically when passing by reference you point to the location of the
variable, and can modify the original

When passing by value, you include a disposable copy of the variable, but it
protects the original.

The "ref" keyword lets you write as if passing by value, but it does the
work of passing by refence for you?

The "out" keyword appears to do the same thing, reading the C# spec has not
shown me the main difference, is there a difference? or does the out keyword
just stomp on whatever the value was with a new value, then goes ahead?
Nov 16 '05 #1
5 1595
Okay, after I ask... using the OUT keyword appears to force the compiler to
expect the method to modify that variable, or it wont compile
"Bradley1234" <so*****@yahoo.com> wrote in message
news:rbwtd.4633$2U2.3163@trnddc01...
Typically when passing by reference you point to the location of the
variable, and can modify the original

When passing by value, you include a disposable copy of the variable, but it protects the original.

The "ref" keyword lets you write as if passing by value, but it does the
work of passing by refence for you?

The "out" keyword appears to do the same thing, reading the C# spec has not shown me the main difference, is there a difference? or does the out keyword just stomp on whatever the value was with a new value, then goes ahead?

Nov 16 '05 #2
Bradley1234 <so*****@yahoo.com> wrote:
Typically when passing by reference you point to the location of the
variable, and can modify the original

When passing by value, you include a disposable copy of the variable, but it
protects the original.

The "ref" keyword lets you write as if passing by value, but it does the
work of passing by refence for you?

The "out" keyword appears to do the same thing, reading the C# spec has not
shown me the main difference, is there a difference? or does the out keyword
just stomp on whatever the value was with a new value, then goes ahead?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Use "out" and it has to be initialized on the way back out of the call, i.e.
*by* the callee. It guarantees that when the method returns, there will
definitely be *some* value assigned to it, when there wasn't necessarily
before the function was called.

Use "ref" it has to be initialized *both* ways, on the way in *and* on the
way out. But since it isn't going to become uninitialized by the function,
this effectively means it just has to be initialized by the caller. The
callee doesn't have to assign anything to it if it doesn't want to, because
it knows it is already assigned.

Note that with "out" the called function isn't allowed to read the value
either until it's assigned something to it.
Note also that due to the above, "out" only requires half the amount of
marshalling as "ref" and is thus faster.

"Bradley1234" wrote:
Okay, after I ask... using the OUT keyword appears to force the compiler to
expect the method to modify that variable, or it wont compile
"Bradley1234" <so*****@yahoo.com> wrote in message
news:rbwtd.4633$2U2.3163@trnddc01...
Typically when passing by reference you point to the location of the
variable, and can modify the original

When passing by value, you include a disposable copy of the variable, but

it
protects the original.

The "ref" keyword lets you write as if passing by value, but it does the
work of passing by refence for you?

The "out" keyword appears to do the same thing, reading the C# spec has

not
shown me the main difference, is there a difference? or does the out

keyword
just stomp on whatever the value was with a new value, then goes ahead?


Nov 16 '05 #4
Thanks Bonj, it makes sense until this part,
Use "ref" it has to be initialized *both* ways, on the way in *and* on the
way out. But since it isn't going to become uninitialized by the function,
this effectively means it just has to be initialized by the caller. The
callee doesn't have to assign anything to it if it doesn't want to, because it knows it is already assigned.

So I understand ref must be used with a variable that was already
initialized, but you said "*and* on the way out" ?

But then the callee doesnt have to assign anything to it, I tried sample
code and noticed this, it doesnt care if it gets assigned or not.

I was hoping this was a useful shortcut for having to deal with pointers,
this looks like an awesome feature.

And thanks, Jon for that webpage, Ill reference that and add a bookmark

Note that with "out" the called function isn't allowed to read the value
either until it's assigned something to it.
Note also that due to the above, "out" only requires half the amount of
marshalling as "ref" and is thus faster.

"Bradley1234" wrote:
Okay, after I ask... using the OUT keyword appears to force the compiler to expect the method to modify that variable, or it wont compile
"Bradley1234" <so*****@yahoo.com> wrote in message
news:rbwtd.4633$2U2.3163@trnddc01...
Typically when passing by reference you point to the location of the
variable, and can modify the original

When passing by value, you include a disposable copy of the variable, but
it
protects the original.

The "ref" keyword lets you write as if passing by value, but it does
the work of passing by refence for you?

The "out" keyword appears to do the same thing, reading the C# spec has not
shown me the main difference, is there a difference? or does the out

keyword
just stomp on whatever the value was with a new value, then goes

ahead?


Nov 16 '05 #5
"out" is a more specific form of "ref". By giving the compiler more
information you allow it to make more checks for you. By designating a
parameter as "out" you are telling the compiler, "I guarantee that when
this method returns, this argument will have been set to a value by the
method."

The compiler then does the necessary static checking to ensure that
your method is living up to its contract.

"ref" is more vague: it states that the method may or may not choose to
assign a (new) value to the variable.

You should use "out" whenever your design calls for multiple return
values from a method, because you then get "free" code correctness
checking. It is much less often that you need "ref". If you just use
"ref" all over the place the compiler won't care, but then you give up
the opportunity to find bugs at compile time rather than at run time.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
2
by: kevin | last post by:
hi there I am struggling with javascript. Can you please help me I've managed to get the get URL in flash to open a html that has the function that opens the Feedback form I want. I Couldn't...
9
by: Daz | last post by:
Hello hello! I'm trying to finish off putting my design into HTML and I've come across a problem that I can't get my head around. I've got divs floating in two columns, but I'm having problems...
14
by: HP | last post by:
Hi All i have confussion regarding given problem please help me out 4. What happens with the following program: void main(){ myclass* pmc = new myclass; pmc = 0; delete pmc;}
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
4
by: John | last post by:
Hi all, I have posted this type of question quite a few times but to date, no-one has actually been able to provide me with a solution. I really need to understand how to do this properly. My...
2
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b)...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.