473,413 Members | 1,811 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,413 software developers and data experts.

Using 'ref' on a reference type

Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.
Jul 25 '06 #1
9 1869
Edward Diener <ediener@no_spam_incomm.comwrote:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.
Sure.

Both reference types and value types, when passed as parameters, are
passed by value - the difference is, with reference types, it's a
reference to the value (i.e. the object on the heap) that's passed by
value.

So, given the following, where Foo is a reference type:

void F(Foo x)
{
x = null; // ok because 'x' is a reference passed by value
}

When you understand this, it's obvious that ref / out are fine with
reference types. They simply cause the reference to the value on the
heap to be passed by reference itself. I.e.:

void F(out Foo x)
{
x = null; // affects caller
}

Foo a = new Foo();
F(out a);
Debug.Assert(a == null); // is true

-- Barry

--
http://barrkel.blogspot.com/
Jul 25 '06 #2
I'm afraid not. ref modifier really says "don't copy this", not "get the
reference of this".
"Edward Diener" <ediener@no_spam_incomm.comha scritto nel messaggio
news:e9**************@TK2MSFTNGP03.phx.gbl...
Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.

Jul 25 '06 #3
Edward Diener <ediener@no_spam_incomm.comwrote:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.
Well, you use ref to pass a parameter by reference. That's not quite
the same as creating *a* reference in the normal .NET sense of a
reference. However, you can certainly pass reference type parameters by
reference too.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 25 '06 #4
Hi,
"Barry Kelly" <ba***********@gmail.comwrote in message
news:8g********************************@4ax.com...
Edward Diener <ediener@no_spam_incomm.comwrote:
>Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.

Sure.

Both reference types and value types, when passed as parameters, are
passed by value - the difference is, with reference types, it's a
reference to the value (i.e. the object on the heap) that's passed by
value.
If I understand correctly this is not what the OP wants, he wants to keep in
a variable the reference to a reference type.

like this:

object o = new object(); //reference type

ref_o = &o; // C syntax.

in C now ref_o has a reference to o , not to the instance being reference
by o;

So the correct answer is no, you cannot have this.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 25 '06 #5
Yes, we can use..

http://msdn2.microsoft.com/en-us/library/s6938f28.aspx

Hope this helps

Veera.

"Edward Diener" wrote:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.
Jul 25 '06 #6
"Laura T" <laura.t@_yahoo.comwrote:
I'm afraid not. ref modifier really says "don't copy this", not "get the
reference of this".
I think you're talking about something else? 'ref' strictly means "pass
by reference rather than pass by value". It's implemented with managed
pointers, using the 'ldloca' instruction (load local address).

-- Barry

--
http://barrkel.blogspot.com/
Jul 25 '06 #7
Barry Kelly wrote:
Edward Diener <ediener@no_spam_incomm.comwrote:
>Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.

Sure.

Both reference types and value types, when passed as parameters, are
passed by value - the difference is, with reference types, it's a
reference to the value (i.e. the object on the heap) that's passed by
value.

So, given the following, where Foo is a reference type:

void F(Foo x)
{
x = null; // ok because 'x' is a reference passed by value
}

When you understand this, it's obvious that ref / out are fine with
reference types. They simply cause the reference to the value on the
heap to be passed by reference itself. I.e.:

void F(out Foo x)
{
x = null; // affects caller
}

Foo a = new Foo();
F(out a);
Debug.Assert(a == null); // is true
Thanks ! What you wrote is what I thought should be the result of
passing a reference type by reference. It is obviously useful when one
wants to return more than one value from a function and the values are
reference types, or the normal return value is a value type and at least
one of the others is a reference type. I am glad to see that MS got this
right in C#.
>
-- Barry
Jul 25 '06 #8
"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>
wrote:
So the correct answer is no, you cannot have this.
According to the OP's himself (parallel to your own), it appears I was
right :)

-- Barry

--
http://barrkel.blogspot.com/
Jul 25 '06 #9
Laura T <laura.t@_yahoo.comwrote:
I'm afraid not. ref modifier really says "don't copy this", not "get the
reference of this".
Well, only sort of. It means "pass the parameter by reference instead
of by value". It's certainly valid to pass a reference-type parameter
by reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 25 '06 #10

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

Similar topics

10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
7
by: Robert Lario | last post by:
For examples sake I have made up a very simple example. I have an object called foo1 which is of type foo. I want to be able to call a funtion called myfunc as follows: myfunc(ref foo1) ...
4
by: Edward Diener | last post by:
My undersstanding of using a ref parameter is to be able to pass initialized value variables as references. Is there any purpose to using a ref paraneter with a reference variable ? If one does it...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
4
by: Elad Gutman | last post by:
Hello, Something is quite puzzled to me in using the 'ref' keyword, probably due to some lack of basic understanding. Whenever I wanna change the parameter passed to a function I will use the...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
9
by: John | last post by:
I'm sorry if this is sounding like somewhat of a noob question. I'm loading in a large binary array of 8x8 double precision floating point matrices, right now this is defined something like ...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
65
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
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?
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
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.