copy a class, C#

后端 未结 12 1788
失恋的感觉
失恋的感觉 2020-12-03 10:13

Is there a way to copy a class in C#? Something like var dupe = MyClass(original).

12条回答
  •  我在风中等你
    2020-12-03 10:44

    Not all classes have this functionality. Probably, if a class does, it provides a Clone method. To help implement that method for your own classes there's a MemberwiseClone protected method defined in System.Object that makes a shallow copy of the current instance (i.e. fields are copied; if they are reference types, the reference will point to the original location).

提交回复
热议问题