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

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.MemberwiseClone());"), or is it more like a
deep copy ("return (BinaryFormatter1.Deserialize(memStream));")?

RL

Aug 12 '07 #1
5 2077
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.MemberwiseClone());"), or is it more like a
deep copy ("return (BinaryFormatter1.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********@yahoo.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.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.MemberwiseClone());"), or is it more like a
deep copy ("return (BinaryFormatter1.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.comwrote:
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.comwrote:

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
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...
2
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 * >>>...
2
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:...
2
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...
3
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...
4
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'...
4
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. ...
4
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-...
9
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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...

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.