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 me
String.Copy
returns a new String
and does not yield the same results as
String copy = otherString;
Try this:
using System;
class Program
{
static void Main()
{
String test = "test";
String test2 = test;
String test3 = String.Copy(test);
Console.WriteLine(Object.ReferenceEquals(test, test2));
Console.WriteLine(Object.ReferenceEquals(test, test3));
Console.ReadLine();
}
}
When you set test2 = test
these references point to the same String
. The Copy
function returns a new String
reference that has the same contents but as a different object on the heap.
Edit: There are a lot of folks that are pretty upset that I did not answer the OP's question. I believe that I did answer the question by correcting an incorrect premise in the question itself. Here is an analogous (if not oversimplified) question and answer that will hopefully illustrate my point:
Question:
I have observed that my car has two doors, one on each side of the car. I believe it to be true that regardless of which door I use I will end up sitting in the driver's seat. What is the purpose of the other door?
Answer:
Actually it is not true that if you use either door you will end up in the driver's seat. If you use the driver's side door you will end up in the driver's seat and if you use the passenger's side door you will end up in the passenger's seat.
Now in this example you could argue that the answer is not really an answer as the question was "what is the purpose of the passenger's side door?". But since that question was wholly based on a misconception of the how the doors worked does it not follow that the refutation of the premise will shed new light on the purpose of the other door by deduction?
string a = "test";
string b = a;
//Object.ReferenceEquals(a,b) is true
a += "more";
//Object.ReferenceEquals(a,b) is now false !
auto-change detection ?