Is there a way to copy a class in C#? Something like var dupe = MyClass(original).
C# – copy a class, C#
c++
Related Question
- C# – Deep cloning objects
- C# – How to copy the contents of one stream to another
- C# – Get int value from enum in C#
- C# – Multiline string literal in C#
- C# – JavaScriptSerializer – JSON serialization of enum as string
- C# – Group by in LINQ
- C# – reason for C#’s reuse of the variable in a foreach
- C# – Why not inherit from List
Best Solution
You are probably talking about a deep copy (deep copy vs shallow copy)?
You either have to:
[Serializable]
attribute.To get a shallow copy, you can use the
Object.MemberwiseClone()
method, but it is a protected method, which means you can only use it from inside the class.With all the deep copy methods, it is important to consider any references to other objects, or circular references which may result in creating a deeper copy than what you wanted.