TL;DR:
Don't use either method:
Clone
doesn't actually do anything.
Copy
is officially obsolete.
Etc:
I'll preface my answer by saying that because String
values are immutable there aren't really many (or any?) good reasons for using either String.Clone()
or String.Copy
.
as per what i have searched aboout is that clone()
makes a new string and copy()
just do copy an existing string and use it
Sorry but your understanding is incorrect.
As per the documentation (emphasis mine):
String.Clone()
does not create a copy of the string, in fact it just returns a self-reference:
https://learn.microsoft.com/en-us/dotnet/api/system.string.clone?view=net-6.0
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.
As for String.Copy()
:
https://learn.microsoft.com/en-us/dotnet/api/system.string.copy?view=net-6.0
The Copy
method returns a String
object that has the same value as the original string but represents a different object reference.
[...]
Starting with .NET Core 3.0, this method is obsolete. However, we do not recommend its use in any .NET implementation. In particular, because of changes in string interning in .NET Core 3.0, in some cases the Copy method will not create a new string but will simply return a reference to an existing interned string.
Additionally, because String.Copy
might create a copy (depending on how interning works)_ it means it's a O(n)
operation.