3

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?

Community
  • 1
  • 1
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • You're right, `string` is a reference type, but it is also immutable, so it acts a little bit different than any other reference type. However, it doesn't change the fact, that `Clone` method is nor really useful. – MarcinJuraszek Oct 07 '13 at 09:30
  • Because `string.SubString(0)` does not return a copy, but the same reference passed to it, unintuitively. – leppie Oct 07 '13 at 09:38

2 Answers2

2

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

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
1

Maybe just to implement the ICloneable interface. Please see ICloneable.Clone Method.

Furthermore, the ICloneable interface seems to be obsolete: