- Home /
How to copy class
How do i copy class? I use C# in Monodevelop. I can't use serializing techniques. Any other technique to copy class?
Answer by jkpenner · Oct 12, 2014 at 10:25 PM
If your just trying to copy the data from one class to a new class you can create a Copy method in the your class. The following example just creates a new instance of the class then assigns the class's values to the new instance and returns it.
Example
class Test {
string name;
int length;
int width;
public Test Copy() {
Test copy = new Test();
copy.name = this.name;
copy.length = this.length;
copy.width = this.width;
return copy;
}
}
That's it. For reasons that become apparent the deeper your reference and inheritance structure goes there is no default deep copy available.
$$anonymous$$ust this be inside the class? or can I perform a copy in any class?
Your answer
Follow this Question
Related Questions
My compiler Monodevelop doesn't build. 1 Answer
Why do Two Instances of MonoDevelop Open when I double-click a CS file in Inspector? 0 Answers
`System.IO.File' does not contain a definition for `AppendText'? 1 Answer
Problem with camera transitions (slerp) 0 Answers
Problem with Camera Transition (First-person to third-person, with Slerp) 1 Answer