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

Passing a byte[] by ref, and here I thought I knew what was going on.

P: n/a
If I call this method, and pass it a byte[] by ref, and initialize
another byte array, set the original equal to it, and then null the
reference, why is the original byte array not null as well? I thought
passing by reference, your passing the address in memory.

public bool DoSomething(ref byte[] data)
{
byte[] retVal = null;

try
{
retVal = new byte[data.Length];

data = retVal;
}
catch (Exception ex)
{
}
finally
{
retVal = null;
}
}

Jan 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
retVal and data are 2 different references (pointers)
data = retVal sets both to reference the same array (i.e. the pointer
memory addresses are equal), you have 2 pointers to the same object
retVal = null clears the retVal reference, but won't touch the data
reference.

If you set data = null then the array reference passed in would be
null

On Jan 30, 4:25 pm, "DaTurk" <mmagd...@hotmail.comwrote:
If I call this method, and pass it a byte[] by ref, and initialize
another byte array, set the original equal to it, and then null the
reference, why is the original byte array not null as well? I thought
passing by reference, your passing the address in memory.

public bool DoSomething(ref byte[] data)
{
byte[] retVal = null;

try
{
retVal = new byte[data.Length];

data = retVal;
}
catch (Exception ex)
{
}
finally
{
retVal = null;
}
}
Jan 30 '07 #2

P: n/a
Hi DaTurk,

yes, passing by reference is passing an address in memory. The address of a
variable wich stores a reference to an array of byte.

Talking about reference could be misleading in C#. You could better think of
a ref and out parameter as passing a variable not a value.

So data here is a (reference type) variable, wich original belonged to the
caller.
retValue is another (reference type) variable, wich is local to this method

In the first line inside the try block you create a new array of byte and
put a reference to it in the variable retVal
After that you copy this reference to th variable data.
In the finally block you put null into the variable retVal; the variable
data is not effected by this.
"DaTurk" <mm******@hotmail.comschrieb im Newsbeitrag
news:11**********************@s48g2000cws.googlegr oups.com...
If I call this method, and pass it a byte[] by ref, and initialize
another byte array, set the original equal to it, and then null the
reference, why is the original byte array not null as well? I thought
passing by reference, your passing the address in memory.

public bool DoSomething(ref byte[] data)
{
byte[] retVal = null;

try
{
retVal = new byte[data.Length];

data = retVal;
}
catch (Exception ex)
{
}
finally
{
retVal = null;
}
}

Jan 30 '07 #3

P: n/a
So, let me see if I'm understanding, thanks for the responses by the
way, your not actually passing the address to the data in memory.
Your passing the address, of the pointer to the actual address in
memory, meaning your passing the variable by ref. I get it.

Feb 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.