From String.Clone() on MSDN:
The return value is not an independent copy of this instance; it is simply another view of the same data. Use the Copy or CopyTo method to create a separate String object with the same value as this instance.
Because the Clone method simply returns the existing string instance, there is little reason to call it directly.
It is my understanding that String is a reference type meaning that you will only ever get a reference to the string object when calling a string.
Therefore why does String.Clone() exist? What is it's purpose?
This is useful since string implements ICloneable, so you can create a copy of clones for a collection of ICloneable items. This is boring when the collection is of strings only, but it's useful when the collection contains multiple types that implement ICloneable. As for copying a single string it has no use, since it returns by design a reference to itself.
Refer : what's the use of string.Clone()?
The Clone() method returns a reference to the string which is being cloned. It is not an independent copy of the string on the Heap. It is another reference on the same string.
and read this article : Explanation for Clone with examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With