473,403 Members | 2,293 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,403 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 1915
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.