Home > Code Generation > Declaring methods and constructors using CodeRush

Declaring methods and constructors using CodeRush

September 21st, 2012

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:

CodeRush Declare Constructor Preview

it will produce the following code:

CodeRush Declare Constructor Result

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:

CodeRush Declare Method Preview

and get the method declared automatically:

CodeRush Declare Method Result

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:

CodeRush Declare Method Abstract Preview

resulting in an abstract method declaration:

CodeRush Declare Method Abstract Result

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
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Sep/21/2012
ID: C187

Similar Posts: