473,761 Members | 10,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays that are passed by reference

If I don't specify "ref" in the argument list when passing an array to the
callee, I am passing the array (reference) by value. But this makes me
confused because it actually means a "reference" of a "reference" ??As I pass
the array by value, the callee can change that array. However,when I use
ref, the callee and caller point to two different arrays. So could anyone
please explain the 2 different situations?
Jun 12 '07 #1
9 2447
On Jun 12, 11:26 am, "Jack" <j...@knight.co mwrote:
If I don't specify "ref" in the argument list when passing an array to the
callee, I am passing the array (reference) by value.
Yes.
But this makes me
confused because it actually means a "reference" of a "reference" ??As I pass
the array by value, the callee can change that array.
The callee can change the *contents* of the array.
However,when I use ref, the callee and caller point to two different arrays.
No they don't. Do you have some sample code which makes you think they
do?
So could anyone please explain the 2 different situations?
If you use ref, the callee can change which array the parameter refers
to, and that change will be made to the caller's variable too.

See http://pobox.com/~skeet/csharp/parameters.html for more
information.

Jon

Jun 12 '07 #2
"Jack" <jl@knight.comw rote in message
news:OT******** ******@TK2MSFTN GP03.phx.gbl...
If I don't specify "ref" in the argument list when passing an array to the
callee, I am passing the array (reference) by value. But this makes me
confused because it actually means a "reference" of a "reference" ??As I
pass the array by value, the callee can change that array. However,when I
use ref, the callee and caller point to two different arrays. So could
anyone please explain the 2 different situations?
The array is always passed by reference. When you add the keyword "ref"
you are passing "a reference to the reference". Meaning that when you pass
the array without "ref", the procedure can change the contents of the array,
buy not the array to which the variable is pointing. The latter can be done
if you add "ref".

Jun 12 '07 #3
No; when you use "ref"", the callee and caller *still* point to the
same array. The difference is that in the latter case, if the callee
*re-assigns* the array (i.e. dataArg = new int[5]), then this is
reflected to the caller - i.e. the caller's reference is updated.
Normally this change would be local to the callee, and would not be
reflected to the caller.

For a full discussion, see Jon's pages:
http://www.yoda.arachsys.com/csharp/parameters.html

Marc
Jun 12 '07 #4
It was 404ing... but now it isn't... so ignore me ;-p
Jun 12 '07 #5
On Jun 12, 11:29 am, "Alberto Poblacion" <earthling-
quitaestoparaco ntes...@poblaci on.orgwrote:
If I don't specify "ref" in the argument list when passing an array to the
callee, I am passing the array (reference) by value. But this makes me
confused because it actually means a "reference" of a "reference" ??As I
pass the array by value, the callee can change that array. However,when I
use ref, the callee and caller point to two different arrays. So could
anyone please explain the 2 different situations?

The array is always passed by reference.
No it's not. The reference is passed by value by default. There's a
difference between passing a value type value by reference and passing
a reference by value.

See http://pobox.com/~skeet/csharp/parameters.html for more
information.

Jon

Jun 12 '07 #6

"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:11***** *************** *@j4g2000prf.go oglegroups.com. ..
On Jun 12, 11:26 am, "Jack" <j...@knight.co mwrote:
>If I don't specify "ref" in the argument list when passing an array to
the
callee, I am passing the array (reference) by value.

Yes.
>But this makes me
confused because it actually means a "reference" of a "reference" ??As I
pass
the array by value, the callee can change that array.

The callee can change the *contents* of the array.
>However,when I use ref, the callee and caller point to two different
arrays.

No they don't. Do you have some sample code which makes you think they
do?
The book is called "C# for experienced programmers"
On page 127-130,
Using system;
....
public class ArrayReferenceT est : System.Windows. Forms.Form
{ ...
private void showOutputButto n_Click(
object sender, System.EventArg s e)
{
int[] firstArray = { 1, 2, 3 };

int[] firstArrayCopy = firstArray;

outputLabel.Tex t = "Test passing firstArray reference by value";

outputLabel.Tex t = "\n\n Contents of firstArray " + "before calling
FirstDouble:\n\ t";

for (int i = 0; i < firstArray.Leng th; i++)
outputLabel.Tex t += firstArray[i] + " ";
FirstDouble (firstArray);

outputLabel.Tex t += "\n\nConten ts of firstArray after " + "calling
FirstDouble\n\t ";
for (int i = 0; i < firstArray.Leng th; i++)
outputLabel.Tex t += firstArray[i] + " ";

if ( firstArray == firstArrayCopy )
outputLabel.Tex t += "\n\nThe references refer to the same
array\n";
else
outputLabel.Tex t += "\n\nThe references refer to different
arrays\n";

int [] secondArray = { 1, 2, 3 };

int [] secondArrayCopy = secondArray;

outputLabel.Tex t += "\nTest passing secondArray " +
"reference by reference";

outputLabel.Tex t += "\nContents of secondArray " +
"before calling SecondDouble:\n \t";

for (int i = 0; i < secondArray.Len gth; i++ )
outputLabel.Tex t += secondArray[i] + "\n";

SecondDouble (ref secondArray);

outputLabel.Tex t += "\n\nConten ts of secondArray " +
"after calling SecondDouble:\n \t";

for (int i = 0; i < secondArray.Len gth; i++)
outputLabel.Tex t += secondArray[i] + " ";

if (secondArray == secondArrayCopy )
outputLabel.Tex t +=
"\n\nThe references refer to the same array\n";
else
outputLabel.Tex t +=
"\n\nThe references refer to different arrays\n";

}
void firstDouble (int [] array)
{
for (int i = 0; i < array.Length; i++)
array[i] *= 2;

array = new int[] { 11, 12, 13 };
}

void SeondDouble (ref int[] array)
{
for (int i = 0; i < array.Length; i++)
array[i] *= 2;

array = new int[] { 11, 12, 13 };
}
}
=============== =============== ===
results:
Test passing firstArray reference by value

Contents of firstArray before calling FirstDouble
1 2 3

Contents of firstArray after calling FirstDouble
2 4 6

The references refer to the same array

Test passing secondArray reference by reference

Contents of secondArray before calling
SecondDouble:
1 2 3

Contents of secondArray after calling SecondDouble:
11 12 13

The references refer to different arrays

=============== =============
Thanks
Jack

>
>So could anyone please explain the 2 different situations?

If you use ref, the callee can change which array the parameter refers
to, and that change will be made to the caller's variable too.

See http://pobox.com/~skeet/csharp/parameters.html for more
information.

Jon

Jun 12 '07 #7
On Jun 12, 11:57 am, "Jack" <j...@knight.co mwrote:
However,when I use ref, the callee and caller point to two different
arrays.
No they don't. Do you have some sample code which makes you think they
do?

The book is called "C# for experienced programmers"
On page 127-130,
<snip>

What this shows is that the callee can change the caller's variable's
value. When it says "The references refer to different arrays" is
means "the value of secondArray is different after calling
SecondDouble to the value it had before". It's *not* saying that the
value within the SecondDouble method is different to the value of
secondArray.

Jon

Jun 12 '07 #8
Starting to see some light on the other end after reading your+MS article,
haven't finished it though :)
Thanks
Jack
"Jack" <jl@knight.com¼ ¶¼g©ó¶l¥ó·s»D:u O*************@ TK2MSFTNGP06.ph x.gbl...
>
"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:11***** *************** *@j4g2000prf.go oglegroups.com. ..
>On Jun 12, 11:26 am, "Jack" <j...@knight.co mwrote:
>>If I don't specify "ref" in the argument list when passing an array to
the
callee, I am passing the array (reference) by value.

Yes.
>>But this makes me
confused because it actually means a "reference" of a "reference" ??As I
pass
the array by value, the callee can change that array.

The callee can change the *contents* of the array.
>>However,whe n I use ref, the callee and caller point to two different
arrays.

No they don't. Do you have some sample code which makes you think they
do?

The book is called "C# for experienced programmers"
On page 127-130,
Using system;
...
public class ArrayReferenceT est : System.Windows. Forms.Form
{ ...
private void showOutputButto n_Click(
object sender, System.EventArg s e)
{
int[] firstArray = { 1, 2, 3 };

int[] firstArrayCopy = firstArray;

outputLabel.Tex t = "Test passing firstArray reference by value";

outputLabel.Tex t = "\n\n Contents of firstArray " + "before
calling FirstDouble:\n\ t";

for (int i = 0; i < firstArray.Leng th; i++)
outputLabel.Tex t += firstArray[i] + " ";
FirstDouble (firstArray);

outputLabel.Tex t += "\n\nConten ts of firstArray after " + "calling
FirstDouble\n\t ";
for (int i = 0; i < firstArray.Leng th; i++)
outputLabel.Tex t += firstArray[i] + " ";

if ( firstArray == firstArrayCopy )
outputLabel.Tex t += "\n\nThe references refer to the same
array\n";
else
outputLabel.Tex t += "\n\nThe references refer to different
arrays\n";

int [] secondArray = { 1, 2, 3 };

int [] secondArrayCopy = secondArray;

outputLabel.Tex t += "\nTest passing secondArray " +
"reference by reference";

outputLabel.Tex t += "\nContents of secondArray " +
"before calling SecondDouble:\n \t";

for (int i = 0; i < secondArray.Len gth; i++ )
outputLabel.Tex t += secondArray[i] + "\n";

SecondDouble (ref secondArray);

outputLabel.Tex t += "\n\nConten ts of secondArray " +
"after calling SecondDouble:\n \t";

for (int i = 0; i < secondArray.Len gth; i++)
outputLabel.Tex t += secondArray[i] + " ";

if (secondArray == secondArrayCopy )
outputLabel.Tex t +=
"\n\nThe references refer to the same array\n";
else
outputLabel.Tex t +=
"\n\nThe references refer to different arrays\n";

}
void firstDouble (int [] array)
{
for (int i = 0; i < array.Length; i++)
array[i] *= 2;

array = new int[] { 11, 12, 13 };
}

void SeondDouble (ref int[] array)
{
for (int i = 0; i < array.Length; i++)
array[i] *= 2;

array = new int[] { 11, 12, 13 };
}
}
=============== =============== ===
results:
Test passing firstArray reference by value

Contents of firstArray before calling FirstDouble
1 2 3

Contents of firstArray after calling FirstDouble
2 4 6

The references refer to the same array

Test passing secondArray reference by reference

Contents of secondArray before calling
SecondDouble:
1 2 3

Contents of secondArray after calling SecondDouble:
11 12 13

The references refer to different arrays

=============== =============
Thanks
Jack

>>
>>So could anyone please explain the 2 different situations?

If you use ref, the callee can change which array the parameter refers
to, and that change will be made to the caller's variable too.

See http://pobox.com/~skeet/csharp/parameters.html for more
information.

Jon


Jun 12 '07 #9
Key code is this (where "array" may or may not be "ref")
for (int i = 0; i < array.Length; i++)
array[i] *= 2;

array = new int[] { 11, 12, 13 };
When called without "ref", the values in the existing array are
doubled, and then a new unrelated array is created and assigned to
"array" - but the "array" variable and the "firstArray " variable are
unrelated; after the method ends, the caller just sees the original
array reference (whose contents have been doubled).

When called /with/ "ref", the values are doubled as before, and then a
new array is created and assigned (via some pointer dereferencing that
is a little complex) into the variable, but in this case "secondArra y"
and "array" are one-and-the-same. The "ref" basically means "don't
create your own variable (on the stack) - use this one instead". So
the values are doubled, but are then discarded in preference for the
new array {11, 12, 13}. As such, at the end of the method the caller
can see the updated reference "secondArra y".

The following is also slightly misleading:
int[] firstArray = { 1, 2, 3 };

int[] firstArrayCopy = firstArray;
This is a copy if the variable, /not/ the array (contents).

Marc
Jun 12 '07 #10

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

Similar topics

5
12623
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
19
2849
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
5
3103
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
27
2633
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this to the function: example = new Array("A",2); example = new Array("Q",4); function loopIt(example);
1
3440
by: Kurt Richardson | last post by:
Hi all Sorry to bother you with what is probably a really trivial question for you C++ experts. My programming skill are pretty amateur, but I'm pretty good at VB.NET. However, I'm wanting to realise some of the speed benefits of writing some of my routines in C++ and accessing them from my VB software. I have managed to do this with a few simple routines in
11
8128
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
64
3433
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like pointers. Can someone please explain why? thanks. Zytan
11
3360
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
8
1793
by: jodleren | last post by:
Hi! I have a function, a part of my code which I can use as a function. It will return 2 arrays, and I am wondering what way to do so. Both arrays hold strings, there are no special keys. 1) setting the arrays as globals 2) returnin an array of arrays 3) returning a large array with a known marker to indicate when the 2nd part starts.
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.