By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

What is the purpose of the string.copy method?

P: n/a
Sounds like a stupid question I know. I can tell that they are used to copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);

Or are they essentially the same?
Jan 21 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
CMirandaman <CM*********@discussions.microsoft.com> wrote:
Sounds like a stupid question I know. I can tell that they are used to copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);

Or are they essentially the same?


String.Copy returns a new string with the same content. Assignment uses
the same *reference*.

One potential use of it is to "trim" strings which have been
constructed with StringBuilder. They often have "padding" at the end of
the string (to allow for appending while they're still in
StringBuilder). I've used the equivalent of this in Java a few times -
can't say I've ever used it in .NET.

--
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
Jan 21 '06 #2

P: n/a
Hello CMirandaman,

Essentially the first one ( x = y ) do not copy the string, it just assigns
x the same reference that y has. Meaning it get's a reference to the string
object, not a copy.
While string.Copy makes a real copy of the string.

--
Patrik L÷wendahl [C# MVP]
http://www.lowendahl.net
Sounds like a stupid question I know. I can tell that they are used to
copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);
Or are they essentially the same?

Jan 21 '06 #3

P: n/a
Sorry to be dense about this but is there a practical difference as far as
writing code is concerned? If I run the following code:
string s;
string p;
s = "Hello";
p = s;
s = "Goodbye";
The output is "Hello" so it appears that assignment functions like a copy..
Can you give me an example of when the Copy method would be needed?

"Patrik L├Âwendahl [C# MVP]" wrote:
Hello CMirandaman,

Essentially the first one ( x = y ) do not copy the string, it just assigns
x the same reference that y has. Meaning it get's a reference to the string
object, not a copy.
While string.Copy makes a real copy of the string.

--
Patrik L├Âwendahl [C# MVP]
http://www.lowendahl.net
Sounds like a stupid question I know. I can tell that they are used to
copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);
Or are they essentially the same?


Jan 21 '06 #4

P: n/a
On Sat, 21 Jan 2006 00:05:02 -0800, "CMirandaman"
<CM*********@discussions.microsoft.com> wrote:
Sounds like a stupid question I know. I can tell that they are used to copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);

Or are they essentially the same?


With x = y you get two pointers pointing to the same piece of memory,
remember that strings are reference types:

x-------> "abcdef"
^
|
y----------|
With x = String.Copy(y) you get two different copies of the string
itself:

x-------> "abcdef"
y-------> "abcdef"
rossum

--

The ultimate truth is that there is no ultimate truth
Jan 21 '06 #5

P: n/a
I think the OP has a point since strings are immutable, interned and
automatically newed. If both references refer to strings with the same
content, they may actually refer to the same interned string.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jan 22 '06 #6

P: n/a
No, they don't. Strings are interned only at compile time. At run time
it is perfectly possible (and common) to have two separate strings with
the same contents. (Kudos to Jon Skeet who corrected me on this until
it sunk in.)

Jon answered the OP's question: String.Copy is useful in some very
obscure circumstances having to do with StringBuilder.

I've never used it myself.

Jan 22 '06 #7

P: n/a
Hello.

well, you let S point to a new string object but P still points to the original
one so that's not strange; what you do is just playing with references. And
you are correct in that if you concatinate S instead, like so:

s = "hello";
p = s;
s += " world"

the output for p would be "hello" and for s "hello world" since a new object
is created (strings are immutable) and thus we wouldn't need copying. I can't
give you an practical example on when string.Copy would be needed since I
after 4 years yet have to find one. But that is the technical difference
anyhow.

--
Patrik L÷wendahl [C# MVP]
http://www.lowendahl.net
Sorry to be dense about this but is there a practical difference as
far as
writing code is concerned? If I run the following code:
string s;
string p;
s = "Hello";
p = s;
s = "Goodbye";
The output is "Hello" so it appears that assignment functions like a
copy..
Can you give me an example of when the Copy method would be needed?

"Patrik L÷wendahl [C# MVP]" wrote:
Hello CMirandaman,

Essentially the first one ( x = y ) do not copy the string, it just
assigns
x the same reference that y has. Meaning it get's a reference to the
string
object, not a copy.
While string.Copy makes a real copy of the string.
--
Patrik L÷wendahl [C# MVP]
http://www.lowendahl.net
Sounds like a stupid question I know. I can tell that they are used
to
copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);
Or are they essentially the same?

Jan 22 '06 #8

P: n/a
Looking at Jon's page I see that it is only string literals that are
interned and for
practical purposes strings _act_ as a value type. So I still think the
OP has a valid
point as I have never needed to use String.Copy.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jan 22 '06 #9

P: n/a
Hello Jeff,

I do agree. string.Copy has no real value since the CLR is doing a lot of
work under the covers.

Noteable though is that you have the option to intern you're own strings.

--
Patrik L÷wendahl [C# MVP]
http://www.lowendahl.net
Looking at Jon's page I see that it is only string literals that are
interned and for
practical purposes strings _act_ as a value type. So I still think the
OP has a valid
point as I have never needed to use String.Copy.
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***

Jan 22 '06 #10

P: n/a
A bit artificial but:

class X
{
string s;

public X(string ss) { s = String.Copy(s); }

public F()
{
lock(s) { .... }
}
}

using the supplied string instead of a copy could cause problems

Jan 22 '06 #11

P: n/a
Nick Hounsome <nh***@nickhounsome.me.uk> wrote:
A bit artificial but:

class X
{
string s;

public X(string ss) { s = String.Copy(s); }

public F()
{
lock(s) { .... }
}
}

using the supplied string instead of a copy could cause problems


Yup. I saw some code just the other day which was locking on a string.
Of course, that's a really bad idea in itself IMO, but there we go...

--
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
Jan 22 '06 #12

P: n/a
Many thanks for all the replies to my post. You helped clarify an issue for
me that I just couldn't find spelled out in any documentation.

"CMirandaman" wrote:
Sounds like a stupid question I know. I can tell that they are used to copy
strings. But what is the difference between
x = y;
versus x = String.Copy(y);

Or are they essentially the same?

Jan 23 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.