473,396 Members | 2,020 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.

ref function parameter question

If I pass a reference type (such as DataSet) to a function, I'm assuming
only a reference is passed (not a copy). So there is no need to declare
function parameter as ref for those types?
Example: private void myFunction(ref DataSet data)

If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar to double pointer in C?

I tried to find this out while debugging but I wasn't able to display
the address of variable from watch window (similar to VC++ debugger
displaying the actual address).

Please advise,
Glenn
Nov 16 '05 #1
2 1914
Glenn Lerner <gl**@hotmail.com> wrote:
If I pass a reference type (such as DataSet) to a function, I'm assuming
only a reference is passed (not a copy).
Correct.
So there is no need to declare
function parameter as ref for those types?
Example: private void myFunction(ref DataSet data)
Not if you're happy with pass-by-value semantics of the reference.
Passing the parameter by reference would change the semantics if you
changed the value of the formal parameter within the method.
If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar to double pointer in C?


It will pass the parameter *by* reference.

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 #2
On Mon, 5 Apr 2004 08:05:49 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Glenn Lerner <gl**@hotmail.com> wrote:
If I pass a reference type (such as DataSet) to a function, I'm assuming
only a reference is passed (not a copy).


Correct.
So there is no need to declare
function parameter as ref for those types?
Example: private void myFunction(ref DataSet data)


Not if you're happy with pass-by-value semantics of the reference.
Passing the parameter by reference would change the semantics if you
changed the value of the formal parameter within the method.
If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar to double pointer in C?


It will pass the parameter *by* reference.

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


In other words, it's passing a reference to the parameter, which is a
reference to an object, and if the method changes the reference to
point to another object, the reference used as an actual parameter in
the calling code will also refer to a different object.

It is similar to a double pointer in C, but just a lot more automagic.
Nov 16 '05 #3

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

Similar topics

15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
4
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
12
by: Darwin Lalo | last post by:
I have a lot of code like this: VOID CALLBACK TimerRoutine(PVOID lpParam) { long nTaskid = (long)lpParam; GObj *obj; if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a hash_map,...
6
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
5
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.