Why does String.Clone() returns the original string and not a copy of it?

后端 未结 3 1385
轻奢々
轻奢々 2021-02-18 17:40

Surprisingly, String.Clone() doesn\'t return a copy of a string as String.Copy() would do. Instead, it returns \'this\', the original stri

3条回答
  •  粉色の甜心
    2021-02-18 18:19

    IClonable is somewhat deprecated as it's unclear what "Clone" means from a system-wide standpoint (deep, shallow...). See http://blogs.msdn.com/b/brada/archive/2003/04/09/49935.aspx

    The reference source documents the Clone method with the following comment:

    // There's no point in cloning a string since they're immutable, so we simply return this.

    Interning of strings means that it's hard to collect strings (they can be referenced more than once) which means really making a new copy of string serves only to stress the system. Plus, interning and copying conflict with each other--so the general rule of interning wins.

提交回复
热议问题