Home > Code Generation > Declaring various properties using CodeRush code generation tools

Declaring various properties using CodeRush code generation tools

August 18th, 2011

The consume-first declaration features of CodeRush are a quick way to generate the required code without typing the entire declaration’s code by hand. Once you have a reference to an undeclared member, pressing the CodeRush key allows you to choose a member you would like to declare. Let’s take a look at the property-declaring code generation providers you can use.

Here they are:

Provider Name

Description

Declare Property Generates a property for the selected element reference.
Declare Property (auto-implemented) Generates an auto-implemented property for the selected element reference.
Declare Property (with backing field) Generates a property with backing store for the selected element reference.
Declare Properties Generates auto-implemented properties for this object initializer expression.
Declare Initialized Property Adds an auto-implemented property and initializes it to the parameter at the caret.
Declare Initialized Properties Adds auto-implemented properties and initializes them to each parameter of this constructor or member.
Declare Getter Generates a getter of the target Property for the selected element reference.
Declare Setter Generates a setter of the target Property for the selected element reference.

The first three code generation providers (Declare Property, Declare Property (auto-implemented), Declare Property (with backing field)) generate the usual property at the target location: inside the current class, or inside the referenced class, if the reference has a type qualifier. In both cases, the final position of the property declaration is being chosen using the target picker.

Consider the following code sample:

public class Circle
{
  public static Circle Create()
  {
    Circle circle = new Circle();
    circle.Radius = 10;
    return circle;
  }
}

The Radius identifier references an undeclared element declaration, so we can declare a property for it. Performing the Declare Property code provider will result in the following declaration:

public int Radius
{
  get
  {
    throw new NotImplementedException();
  }
  set
  {
    throw snew NotImplementedException();
  }
}

Choosing the Declare Property (with backing field) will produce the following code:

private int _Radius;
public int Radius
{
  get
  {
    return _Radius;
  }
  set
  {
    _Radius = value;
  }
}

And, finally, Declare Property (auto-implemented) will create a simple property declaration:

public int Radius { get; set; }

Note that if the reference is not assigned to any value, the Declare Property and Declare Property (with backing field) code provider will produce a read-only property. For example, declaring a property for the Radius reference from this code:

public class Circle
{
  public static Circle Create()
  {
    Circle circle = new Circle();
    circle.Diameter = circle.Radius * 2;
    return circle;
  }
  public int Diameter { get; set; }
}

Using the Declare Property (with backing field) will produce a read-only property with the getter only, because the identifier is not assigned and the value is only retrieved in the code:

private int _Radius;
public int Radius
{
  get
  {
    return _Radius;
  }
}

Note that the auto-implemented properties should always declare its setter and getter by the language specification. So, declaring a property from a read-only reference, using the Declare Property (auto-implemented) code provider will always generate a read-write property.

After you have a read-only or write-only property declared, and then write a reference to utilize another property’s accessor, the Declare Setter and Declare Getter code providers may help you create one for you. In the sample above, we declared a read-only property. Here’s the code:

public class Circle
{
  public static Circle Create()
  {
    Circle circle = new Circle();
    circle.Diameter = circle.Radius * 2;
    return circle;
  }
  private int _Radius;
  public int Radius
  {
    get
    {
      return _Radius;
    }
  }
  public int Diameter { get; set; }
}

If you would like to initialize the Radius like this:

public static Circle Create()
{
  Circle circle = new Circle();
  circle.Radius = 10;
  circle.Diameter = circle.Radius * 2;
  return circle;
}

You have to declare a setter for the property. Executing the Declare Setter will extend the Radius read-only property with the getter accessor and the property now has read-write access:

public int Radius
{
  get
  {
    return _Radius;
  }
  set
  {
    _Radius = value;
  }
}

Notice that the setter’s code logic was automatically implemented for you as well. The Declare Getter generates a getter accessor for the write-only property and converts the property into a read-write property.

Now let’s review the Declare Initialized Property and Declare Initialized Properties code providers. The first one declares an auto-implemented property and initializes it to the parameter at the editor caret. Once your editor caret position is at a parameter to a method or constructor, e.g. ‘radius’ in the following sample:

public class Circle
{
  /// <summary>
  /// Initializes a new instance of the Circle class.
  /// </summary>
  /// <param name="radius">The radius of the Circle.</param>
  public Circle(int radius)
  {
  }
}

the Declare Initialized Property becomes available, and when you perform it, it will produce the following result:

public class Circle
{
  /// <summary>
  /// Initializes a new instance of the Circle class.
  /// </summary>
  /// <param name="radius">The radius of the Circle.</param>
  public Circle(int radius)
  {
    Radius = radius;
  }
  public int Radius { get; private set; }
}

The Declare Initialized Properties does absolutely the same thing, but for several parameters. It is available on the name of the method or constructor. If there are at least two parameters that do not have the corresponding properties, it will declare properties for all parameters that do not have an assignment to a property. For example, having this code:

public class Circle
{
  /// <summary>
  /// Initializes a new instance of the Circle class.
  /// </summary>
  /// <param name="radius">The radius of the Circle.</param>
  /// <param name="Color">The color of the Circle.</param>
  public Circle(int radius, Color color)
  {

  }
}

and applying the Declare Initialized Properties will extend your code to the following one:

public class Circle
{
  /// <summary>
  /// Initializes a new instance of the Circle class.
  /// </summary>
  /// <param name="radius">The radius of the Circle.</param>
  /// <param name="Color">The color of the Circle.</param>
  public Circle(int radius, Color color)
  {
    Radius = radius;
    Color = color;
  }
  public int Radius { get; private set; }
  public Color Color { get; private set; }
}

And finally, the Declare Properties code provider. This one, as mentioned earlier,
generates auto-implemented properties for an object initializer expression. Object initializer expression is constructed like this:

public class Circle
{
  public static Circle Create(int radius, Color color)
  {
    return new Circle { Radius = radius, Color = color };
  }
}

Notice that the Radius and Color properties are undefined. Move the editor caret to one of these identifiers and apply the Declare Properties code provider. It will result in the following code:

public class Circle
{
  public static Circle Create(int radius, Color color)
  {
    return new Circle { Radius = radius, Color = color };
  }
  public int Radius { get; set; }
  public Color Color { get; set; }
}

That’s all for property generation code providers. See other code-declaring code providers that are shipped in CodeRush Pro. By the way, some of these providers are available in the free CodeRush Xpress version.

—–
Products: CodeRush Pro, CodeRush Xpress
Versions: 11.1 and up
VS IDEs: any
Updated: Aug/19/2011
ID: C121

Similar Posts: