Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of System.String.Copy in .NET?

I'm afraid that this is a very silly question, but I must be missing something.

Why might one want to use String.Copy(string)?

The documentation says the method

Creates a new instance of String with the same value as a specified String.

Since strings are immutable in .NET, I'm not sure what's the benefit of using this method, as I'd think that

 string copy = String.Copy(otherString); 

would for all practical purposes seem to yield the same result as

 string copy = otherString; 

That is, except for whatever internal bookkeeping that's going on, and the fact that copy is not ReferenceEquals to otherString, there are no observable differences - String being an immutable class whose equality is based on value, not identity. (Thanks to @Andrew Hare for pointing out that my original phrasing was not precise enough to indicate that I realized there was a difference between Copying and not, but was concerned about the perceived lack of useful difference.)

Of course when passed a null argument, Copy throws an ArgumentNullException, and the "new instance" might consume more memory. The latter hardly seems like a benefit, and I'm not sure that the null check is a big enough bonus to warrant a whole Copy method.

Thanks.

like image 510
Blair Conrad Avatar asked Feb 06 '09 16:02

Blair Conrad


People also ask

What is the use of string copy?

C strcpy() The strcpy() function copies the string pointed by source (including the null character) to the destination. The strcpy() function also returns the copied string.

What is System string in C#?

In C#, string is an object of System. String class that represent sequence of characters. We can perform many operations on strings such as concatenation, comparision, getting substring, search, trim, replacement etc.

How do I copy a string in C#?

Copy(String) Method is used to create a new instance of String with the same value as a specified String. In other words, this method is used to copy the data of one string into a new string. The new string contains same data like an original string but represents a different object reference.

What is PowerShell System string?

The PowerShell string is simply an object with a System. String type. It is a datatype that denotes the sequence of characters, either as a literal constant or some kind of variable. A String can be defined in PowerShell by using the single or double-quotes. Both the strings are created of the same System.


2 Answers

With String.Copy you are actually allocating new memory and copying the characters from one string to another; you get a completely new instance as opposed to having both variables being the same instance. This may matter if you use the string with unmanaged code which deals with the memory locations directly and can mutate the string.

like image 118
tvanfosson Avatar answered Oct 19 '22 05:10

tvanfosson


Here's one piece of the puzzle. It doesn't explain why you would want to do it, but it does help explain a functional difference.

If you pin the string using the fixed keyword, the contents would be mutable. Off the top of my head, I can't think of a situation in which you would want to do this, but it is possible.

string original = "Hello World"; string refCopy = original; string deepCopy = String.Copy(original);  fixed(char* pStr = original) {    *pStr = 'J'; }  Console.WriteLine(original); Console.WriteLine(refCopy); Console.WriteLine(deepCopy); 

Output:

Jello World Jello World Hello World 
like image 31
Kennet Belenky Avatar answered Oct 19 '22 05:10

Kennet Belenky