Skip to main content
A newer version of this page is available. .

Declare Class

  • 2 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. The class is declared in a new source code file. This Code Provider is especially useful for Test-Driven Development. It allows you to easily 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 on 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. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Declare Class from the menu.

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();
            }
        }
    }
}
NOTE
  • You can configure where to place the newly-generated type declarations. The possible options are: Above the current type, Below the current type, or Create a new file. Open the Editor | <Language> | Code Actions | Code Actions Settings options page to change this option.

  • The default visibility modifier of the generated type is configured on the Editor | <Language> | Scope Options options page. Refer to the Scope topic for details.