Declaring methods and constructors using CodeRush
When writing a new code, it usually implies writing a not-declared code like a method invocation, property calls, etc. To help you quickly create the required declarations, there are a lot of code generation providers for declaring methods, properties, fields, locals and everything else. Let’s review the method and constructor code declaration providers:
- Declare Constructor
- Declare Method
- Declare Method (abstract)
Declare Constructor
When instantiating a new class with the ‘new’ operator, we may realize that we need additional or a different set of arguments passed to a class constructor. In this case a constructor does not exist yet, of course. The Declare Constructor code provider allows you to create a new constructor at the preferred position, fill out the new parameter names automatically, and allow you to rename them if required.
After the code provider is applied on the following undeclared constructor instantiation:
it will produce the following code:
We can do the same with usual the methods, whether they are static or instance. Just write down the method invocation expression and specify the required arguments for the new method. Then, apply the Declare Method code provider:
and get the method declared automatically:
Another version of the code provider called Declare Method (abstract) does almost the same thing, but creates an abstract method if you are inside an abstract class:
resulting in an abstract method declaration:
The difference with the Visual Studio method code generation feature is that CodeRush code providers have the following benefits:
- choose a destination position for a new method
- specify the name of each parameter easily
- toggle the default method options, e.g., scope (public, private, protected, etc)
- quickly navigate inside the new method and get back if required using Markers