473,586 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What exactly does "=" (equal) equal? A shallow copy?

In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y; //what does this '=' mean?

Is "X=Y" a shallow copy? I think it is.

So is X=Y above equivalent to using ICloneable to produce a shallow
copy clone ("return (this.Memberwis eClone());"), or is it more like a
deep copy ("return (BinaryFormatte r1.Deserialize( memStream));")?

RL

Aug 12 '07 #1
5 2092
Hello raylopez99,
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();
X = Y; //what does this '=' mean?

Is "X=Y" a shallow copy? I think it is.

So is X=Y above equivalent to using ICloneable to produce a shallow
copy clone ("return (this.Memberwis eClone());"), or is it more like a
deep copy ("return (BinaryFormatte r1.Deserialize( memStream));")?

RL
It's neither if MyObject is a class. in that case they become exactly the
same object. Not even a copy, truly the same.

If they're structs however you would get a sort of clone or shallow copy.
All references will just be copied. All value types will be cloned as well.

Jesse
Aug 12 '07 #2

"raylopez99 " <ra********@yah oo.comwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y; //what does this '=' mean?
X is not an instance of MyObject; it is a reference to an instance of
MyObject. Basically X points to an instance.

X = Y // X now points to a different instance.
In C# you can reasign references and even declare null references (you
can get a null reference in C++, but that generally a design flaw)

The sample above would look like this in C++

MyObject *X = new MyObject();
MyObject *Y = new MyObject();

X = Y; //what does this '=' mean?
Simply reasigning a pointer (although it is also a memory leak in C++)

Is "X=Y" a shallow copy? I think it is.
Nope, simply a copy of a reference

In C++ you can do the following (Value semantics)
MyObject X; // X is an instance (on the stack)
MyObject Y; // Y is an instance (on the stack)
X=Y //Shallow copy from the second instance into the first instance

or you can do (reference semantics)

MyObject *X = new MyObject(); // X is a pointer to a instance of
MyObject (on the heap)
MyObject *Y = new MyObject(); // Y is a pointer to a instance of
MyObject (on the heap)

X = Y; // copy a pointer (memory leak)

So
MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y;
The only thing that is copied is the reference. Both X and Y point to
the same instance

Hope this helps
Bill

Aug 12 '07 #3
raylopez99 wrote:
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has.
No, you can't overload the assignment operator in C#.
This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y; //what does this '=' mean?
It means that you copy the value of Y into X. The assignment operator
always does that, with no exception.

A class is a reference type, so the variables X and Y are references.
It's the reference that is copied, not the object. X becomes a reference
to the object that Y is referending.
Is "X=Y" a shallow copy? I think it is.
No, it's not. In .NET there is no automatic copying of objects. If you
want a copy of an object, you have to explicitly create one.
So is X=Y above equivalent to using ICloneable to produce a shallow
copy clone ("return (this.Memberwis eClone());"), or is it more like a
deep copy ("return (BinaryFormatte r1.Deserialize( memStream));")?
Neither. It just copies the reference, not the object.

--
Göran Andersson
_____
http://www.guffa.com
Aug 12 '07 #4
On Aug 12, 6:26 am, Göran Andersson <gu...@guffa.co mwrote:
Thanks, and to Bill Butler and Jesse Houwing as well.
RL

Aug 12 '07 #5
On Aug 12, 6:18 am, "Bill Butler" <qwe...@asdf.co mwrote:

Yes Bill, it was very useful, since I come from a C++ background,
where "data slicing" or "alising" is always a problem and you have to
make copies of stuff.

I see that C# is more like "managed C++" in the CLI

Thank you

RL
Aug 12 '07 #6

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

Similar topics

15
6868
by: lkrubner | last post by:
I want to give users the power to edit files from an easy interface, so I create a form and a PHP script called "fileUpdate". It does a reasonable about of error checking and prints out some errors. It uses fileperms() to get the permissions of the file, and it includes that info in any error message. Today I'm getting the following error...
2
2069
by: Greg Smethells | last post by:
What exactly does "*values" mean in the following code? Is this a pointer to a PyList? Why can I not find good documentation on this any where? I must be blind: >>> from struct import * >>> format = "dl" >>> values = >>> foo = pack(format, *values) >>> foo '\x1f\x85\xebQ\xb8\x1e\t@*\x00\x00\x00'
2
3497
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1 Copying a file: WRONG WAY: #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
2
1553
by: Johann Blake | last post by:
I posted a related problem today. The problem is this: string str1 = @""""; When I execute this code (even in a bare bones application), in the IDE it returns "\""" Why? Even in the immediate window it return this. Anywhere where I attempt to use two apostrophes, it inserts a backslash. Even a function in one method that saves its...
3
5953
by: Dave | last post by:
Greetings All, I have never seen this error before, and after an extensive search of Groups, I cannot find a satisfactory answer to my basic question. What exactly does "is not a member of 'String'" mean specifically? I am developing in VB.NET and I was using the Collection datatype to store property and object values. However, another...
4
3087
by: lander | last post by:
I've read the page life cycle thing in msdn, still, i'm getting a bit confused of thinking how all the things are going under the hood... I know that when page loading, that the controls' properties is populated and when page unloading, the resources are cleared. What I want to know is what's happening behind it, that is, from the...
4
5356
by: Lightmage | last post by:
Hello all, I am using C++ to convert a certain file into an XML file. During the conversion the program does some arithmetic operations on the information that is then outputted in xml format. The information is a double variable. Usually the output is exactly what it should be, but every so often the output is "-1.#J".
4
10816
by: david.karr | last post by:
I'm a CSS newbie, but I was browsing through the css files in the YUI library, and I noticed the following line: body {font:13px/1.231 arial,helvetica,clean,sans-serif;*font- size:small;*font:x-small;} I understand everything but the "/1.231". Is that something like a multiplier on the EM size? I can't find an example of that in the CSS...
9
27614
by: erictheone | last post by:
Ok so what I'm trying to do is create a trans location cipher. For those among us that don't know alot about cryptography it is a method for jumbling up letters to disguise linguistic patterns(words). What it does is takes a string as a parameter, determines length of string, tests if the length is a perfect square, if it is then it makes a 2-d...
0
7912
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...
0
7839
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
8202
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. ...
1
7959
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
8216
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
5710
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
5390
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
3837
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
1180
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.