coderushforroslyn-115689-coding-assistance-code-providers-declaration-providers-declare-class.md
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.
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.
Available when the caret is in a class name if the class does not exist.
Place the caret in a non-existent class name in its construction.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
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();
}
}
}
}
'Filename: Customer.vb
Namespace ConsoleApp
Public Class Customer
Public Sub New(ByVal str1 As String, ByVal str2 As String)
End Sub
Public Sub SaveToDB()
Throw New NotImplementedException
End Sub
Property Age As Integer
Get
Throw New NotImplementedException
End Get
Set(ByVal Value As Integer)
Throw New NotImplementedException
End Set
End Property
End Class
End Namespace
You can configure the “Declare Class” code provider settings on the Editor | C# (Visual Basic) | Code Actions | Code Actions Settings options 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:
For more information, refer to the following topic: Code Actions Settings.
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.
See the following topic for details: Scope.