Reputation:
Is there an easy way to duplicate a class with a different name?
Upvotes: 9
Views: 9840
Reputation: 59
When developing the cloning is the best way to achive final refactoring still having working copy of the origin.
After developing you can just remove unnecessary objects.
Upvotes: -1
Reputation: 428
First of all: now there is the option of "Copy Class" with the MS powertools for VS (marketplace)
Second: to all the critics that know better - there are legitimate cases where you want to copy a class as a starting point. I for example have a base class and from it I derive dozens of classes with different implementations, yet I do have a "reference class" derived from the base which already contains the overides, comments, etc. so I do not have to repeat the same over and over again. This is just one of the many situation where you want to copy a class.
Upvotes: 3
Reputation: 10138
Not sure whether this can be qualified as the easiest way but if you have ReSharper, you can use its Copy Type refactoring to copy classes/interfaces/structs with control over the namespace the copy is landing in and naming within the copy - which means that if you're copying a class with 5 constructors, the copy will have all of them renamed to match the name of the new class.
However, depending on what you're trying to achieve, using Extract Interface or Extract Superclass might be a better option.
Upvotes: 9
Reputation: 2283
It's usually better to refactor your class to be reusable. Copy paste code leads to having to fix the same code in multiple places and much pain as you're breaking OO principles.
Upvotes: 0
Reputation: 838006
No, there's no refactoring tool for duplicating a class but with different name.
I would imagine that the reason why this feature is not present is because duplicating code is generally considered a bad idea. I'd suggest instead changing your class into a base class and then make two derived classes from it, overriding methods where you need to change the behaviour.
Upvotes: 5
Reputation: 6400
Ctrl+A
.Ctrl+C
.Ctrl+V
in the new file.Ctrl+H
)Upvotes: 1
Reputation: 111830
Refactor to give a new name, copy, undo, paste :-) Remember the undo! This will rename the constructors and finalizers!
Upvotes: 6