Skip to main content

Declare Class

  • 3 minutes to read

Purpose

Generates a class for reference to a non-existent class and adds the class members referenced from the initial code to it. This code provider can drop a marker onto the initial class reference, if the class is created in a new file (the default behavior) and the Markers feature is enabled.

Options_Scope

See the following topic section for more details: Markers: How to Enable

The Declare Class code provider is useful for Test-Driven Development. This provider allows you to create declarations for classes referenced in a code fragment.

Note

The Declare Class code provider also declares class members called from the initial code.

Availability

Available when the caret is in a class name if the class does not exist.

Usage

  1. Place the caret in a non-existent class name in its construction.

    Note

    The blinking cursor shows the caret’s position at which the Code Provider is available.

    //Filename: Program.cs
    class Program {
        static void Main() {
            var Andrew = new Customer("Andrew", "Fuller");
            Andrew.Age = 47;
            Andrew.SaveToDB();
        }
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Declare Class from the menu.

    declare-class-item

After execution, the code provider adds a new file to the project and declares the class in it.

//Filename: Customer.cs
using System;

namespace ConsoleApp
{
    public class Customer
    {
        public Customer(string str1, string str2)
        {
        }
        public void SaveToDB()
        {
            throw new NotImplementedException();
        }
        public int Age {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }
    }
}

Customization

Change Code Actions Settings

You can configure the “Declare Class” code provider settings on the Editor | C# (Visual Basic) | Code Actions | Code Actions Settings options page.

Page

For example, specify the “Position of newly-generated type declarations” setting to configure where CodeRush should place the newly-generated type declarations. The possible options are:

  • Above the current type
  • Below the current type
  • Create a new file (the default value)

For more information, refer to the following topic: Code Actions Settings.

Change Scope

You can also change the default visibility modifier of the generated type and its members on the Editor | C# (Visual Basic) | Scope Options options page.

Options_Scope

See the following topic for details: Scope.