473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1882
Edward Diener <ediener@no_spa m_incomm.comwro te:
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_spa m_incomm.comha scritto nel messaggio
news:e9******** ******@TK2MSFTN GP03.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_spa m_incomm.comwro te:
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.co m>
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.c om...
Edward Diener <ediener@no_spa m_incomm.comwro te:
>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_spa m_incomm.comwro te:
>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.mach in 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.co m>
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
2253
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 the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template...
7
5468
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) here's the function : public void myfunc(ref object foo)
4
4147
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 the compiler accepts it. Does this mean anything ?
17
9356
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 commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of...
4
6357
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 'ref' keyword, that's fine, and it even makes sense when sending a Value Type parameter (int, float or a struct, for example). However, if I'm...
6
2833
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 types hold a reference to an object as opposed to object data itself. So, when reference type parameter is passed into a method, a copy of objects...
9
1766
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 struct Mat8x8 { double M11; double M12;
4
1832
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: C:\PK\Development\Products\UTCAS\4.0\SRC\MeltPracApplication\Dialog\Composit ionForm.cs(942): Argument '1': cannot convert from 'ref...
65
3852
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
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
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...
0
6052
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...
0
3505
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...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1953
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
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
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...

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.