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

Home Posts Topics Members FAQ

References vs Copies

Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi

Sep 12 '06 #1
22 1639
Yes, that is exactly what is happening - classes have reference-type
semantics, so your "variable" (address) is preceisly that: a (managed)
pointer to an object on the (managed) heap.

If you want value-type semantics (i.e. instances cloned at the blink of an
eye) then "struct" can be useful, although to be honest an address "feels"
like a class to me. Perhaps look at the ICloneable interface? i.e.

public class Address : ICloneable {
public Address Clone() { // your explicit copy code goes here
}
object ICloneable.Clon e() {return this.Clone();} // non-typesafe version
usually done via an explicit implementation
}

then you can call:
Address addr = ei.Address.Clon e();
Sep 12 '06 #2
Hi,

"mehdi_mous avi" <me***********@ gmail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

In managed world it's called reference but yes, that's pretty much what
happens IF Address IS A class ; in your post you especify that ei is an
instance of a class, you say nothing about Address.

If Address is not a class but a struct the above is not true.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 12 '06 #3
There are 2 distinct ways to reference data in .Net: By value, and by
reference. This is why the .Net platform defines reference types and value
types. Value types, such as integers and structures, are passed "by value,"
meaning that a copy of the data in the type is passed when an instance of
that type is referenced. Reference types, which constitute most of the .Net
types, are passed "by reference," meaning that a managed pointer to the
instance is passed when an instance of that type is referenced.

To pass a copy of a reference type, the instance must be copied, or Cloned.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"mehdi_mous avi" <me***********@ gmail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi

Sep 12 '06 #4
No - value types can be passed by value or by reference and reference types
can be passed by value or by reference.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Kevin Spencer" wrote:
There are 2 distinct ways to reference data in .Net: By value, and by
reference. This is why the .Net platform defines reference types and value
types. Value types, such as integers and structures, are passed "by value,"
meaning that a copy of the data in the type is passed when an instance of
that type is referenced. Reference types, which constitute most of the .Net
types, are passed "by reference," meaning that a managed pointer to the
instance is passed when an instance of that type is referenced.

To pass a copy of a reference type, the instance must be copied, or Cloned.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"mehdi_mous avi" <me***********@ gmail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi


Sep 12 '06 #5
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi
See http://www.yoda.arachsys.com/csharp/parameters.html
Sep 12 '06 #6
"No?" No to what?

If I told you that 1 + 1 = 2, would you say "No - 2 + 0 = 2"?

The fact that 2 statements concerning anything state different things
doesn't imply that one of the 2 statements is wrong.

In fact, the only wrong statement so far has been your "No".

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:3F******** *************** ***********@mic rosoft.com...
No - value types can be passed by value or by reference and reference
types
can be passed by value or by reference.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Kevin Spencer" wrote:
>There are 2 distinct ways to reference data in .Net: By value, and by
reference. This is why the .Net platform defines reference types and
value
types. Value types, such as integers and structures, are passed "by
value,"
meaning that a copy of the data in the type is passed when an instance of
that type is referenced. Reference types, which constitute most of the
.Net
types, are passed "by reference," meaning that a managed pointer to the
instance is passed when an instance of that type is referenced.

To pass a copy of a reference type, the instance must be copied, or
Cloned.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"mehdi_mousavi " <me***********@ gmail.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi



Sep 12 '06 #7
You had said that value types are passed by value and reference types are
passed by reference. This is the part that is obviously not correct.
To be clear, you could have said that value types *can* be passed by value
and that reference types *can* be passed by reference, but from your post, it
was pretty obvious that this isn't what you intended.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Kevin Spencer" wrote:
"No?" No to what?

If I told you that 1 + 1 = 2, would you say "No - 2 + 0 = 2"?

The fact that 2 statements concerning anything state different things
doesn't imply that one of the 2 statements is wrong.

In fact, the only wrong statement so far has been your "No".

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:3F******** *************** ***********@mic rosoft.com...
No - value types can be passed by value or by reference and reference
types
can be passed by value or by reference.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Kevin Spencer" wrote:
There are 2 distinct ways to reference data in .Net: By value, and by
reference. This is why the .Net platform defines reference types and
value
types. Value types, such as integers and structures, are passed "by
value,"
meaning that a copy of the data in the type is passed when an instance of
that type is referenced. Reference types, which constitute most of the
.Net
types, are passed "by reference," meaning that a managed pointer to the
instance is passed when an instance of that type is referenced.

To pass a copy of a reference type, the instance must be copied, or
Cloned.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"mehdi_mous avi" <me***********@ gmail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore, any
modification on addr variable is instantly applied to the ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi



Sep 12 '06 #8
mehdi_mousavi wrote:
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of Address
type.

The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address.
You say "unlike C/C++", but this is not necessarily so. In C, if the
Address field of ei is a pointer, then in fact addr will contain the
same pointer and any modifications to addr->Blah will also change
ei.Address->Blah.

Others here have given good answers, but I wanted to adapt them to your
situation. What will happen in this situation depends upon two things:

1) Whether the type Address is a class or a struct.
2) Whether the Address member of ei is a field, a property that returns
a reference that is part of the state of ei, or a property that returns
a clone of the Address held by ei.

Most here have talked about point #1. Specifically, if the Address type
is a struct--that is, a value type--then assignment and/or returning an
Address from a method implies copying it.

public struct Address
{ ... }

So, if this were the case then changes to the Address held by addr
would not affect the Address held by ei. This is regardless of point #2
above.

If the Address type is a class, then point #2 comes into play: is
ei.Address a field, or is a property. If it's a property, does it
return a reference held by ei or return a clone?

If the Address type is a class and ei.Address is a field:

public class Address
{ ... }

public class EiType
{
public Address Address;
}

then yes, addr will refer to the same object instance as does
ei.Address, and changes to one will be visible when referring to that
same object via the other reference. In effect the "two" will appear to
change in synch, because in reality there's only one.

Similarly, if the field is wrapped in a property that just returns the
object reference, the same applies:

public class Address
{ ... }

public class EiType
{
private Address _address;

public Address Address { get { return this._address; } }
}

However, if ei.Address is a property that returns a clone:

public class Address : ICloneable
{
public override object Clone() { ... }
}

public class EiType
{
private Address _address;

public Address Address { get { return
(Address)this._ address.Clone() ; } }
}

then the reference assigned to addr will be to a different object
instance than the one held by ei._address, and changes to one will not
be reflected in the other.

Sep 12 '06 #9
I intended to say exactly what I said. If I had intended to say otherwise, I
would have. That is why my software works well.

Value types *are* passed by value. Passing them by reference is a (rather
rare) exception (unless the developer is unskilled), and requires
instructions that specify that it should be done that way. Same thing with
reference types, only moreso. I honestly can't think of any time I've tried
to pass a reference type by value. But hey, if it makes you feel superior to
correct people who are not wrong, by all means, do so.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:0B******** *************** ***********@mic rosoft.com...
You had said that value types are passed by value and reference types are
passed by reference. This is the part that is obviously not correct.
To be clear, you could have said that value types *can* be passed by value
and that reference types *can* be passed by reference, but from your post,
it
was pretty obvious that this isn't what you intended.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Kevin Spencer" wrote:
>"No?" No to what?

If I told you that 1 + 1 = 2, would you say "No - 2 + 0 = 2"?

The fact that 2 statements concerning anything state different things
doesn't imply that one of the 2 statements is wrong.

In fact, the only wrong statement so far has been your "No".

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:3F******* *************** ************@mi crosoft.com...
No - value types can be passed by value or by reference and reference
types
can be passed by value or by reference.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Kevin Spencer" wrote:

There are 2 distinct ways to reference data in .Net: By value, and by
reference. This is why the .Net platform defines reference types and
value
types. Value types, such as integers and structures, are passed "by
value,"
meaning that a copy of the data in the type is passed when an instance
of
that type is referenced. Reference types, which constitute most of the
.Net
types, are passed "by reference," meaning that a managed pointer to
the
instance is passed when an instance of that type is referenced.

To pass a copy of a reference type, the instance must be copied, or
Cloned.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"mehdi_mousavi " <me***********@ gmail.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
Hi folks,
Consider the following line of code:

Address addr = ei.Address;

where ei is an instance of a class that has got a property of
Address
type.

The question is that what is happening in the above assignment?
Unlike
the C/C++, it seems that the above code instructs the compiler to
point
the addr variable to the same location where ei.Address already
resides, i.e, it's somehow a pointer to the ei.Address. Therefore,
any
modification on addr variable is instantly applied to the
ei.Address.

Well, is this right? or, am I missing something?

Any help would be highly appreciated,
Cheers,
Mehdi




Sep 12 '06 #10

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

Similar topics

4
1240
by: Thomas Philips | last post by:
I want to represent an NxN matrix by a list containing N lists, each of which has N elements. Initially the elements are set to " ". For N=2, I write >>>x = *2 #assignment creates references, not copies! >>>x >>>y = ]*2 >>>y , ]
9
1672
by: Henning Kage | last post by:
I'm using Python only for some months now and I'm wondering, whether such assignments as above are creating bitwise copies of an object or just recieve a reference. That means I wanted to know, wheter Python in general differs between references and copies: class someclass: def __init__( self, otherobject): self.someattribute = otherobject...
4
1545
by: Andrew | last post by:
Can someone help explain the following? Why is it that elements 5 - 9 in Bar::group do not seem to resolve to elements 0 - 4 of Bar::group when de-referenced? If I am following this correctly I would assume the following is happening. Elements 0 - 4 of Bar::group are created when Bar mainlist is declared in main.
4
1250
by: Scott Danzig | last post by:
I'm curious.. let's say you had: int val = 5; int a() { return val; } int &b() {
2
32171
by: Michelle Collier-Moore | last post by:
Please could someone offer some advice regarding adding references to an Access database? I tried to open a project a few days ago sent to me by someone whose developer had left the company. I found that I had a reference problem and went to the usual Tools, References place to fix it. The References option was greyed out and as I have...
2
8867
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
4
4593
by: naveid | last post by:
I have an array (List<T>) containing tens of thousands of items. I need to maintain copies of the array, sorted in a few different ways. I'm working on Windows Mobile so memory is a constraint. To minimize memory requirements, in C++, I can have arrays of pointers to the first array (T*). I understand I can do this in C# as well, but the code...
8
1641
by: howa | last post by:
from PHP manual, it said: Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own ------------------ Why?
3
1485
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .... .... return "f1" + arg ....
0
187
by: Calvin Spealman | last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote: As was pointed out already, this is a basic misunderstanding of assignment, which is common with people learning Python. To your actual problem... Why do you wanna do this anyway? If you want to change the function in the dictionary, why don't you simply define the functions...
0
7614
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
7924
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
8125
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...
1
7676
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...
0
7974
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...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.