Create Descendant
- 4 minutes to read
Purpose
Creates a descendant class/record with a default constructor or constructors and overrides selected non-implemented abstract and virtual members of a base class/record.
If the base class or record does not contain abstract and virtual members that must be overridden in a descendant class/record, this code provider creates only a descendant class/record with a default constructor.
This code also adds stubs for overridden members. Depending on your settings, stubs can throw a new NotImplementedException() (the default behavior), return a default value, or call an undeclared method that causes a compilation error. See the Customization section for more information.
The Create Descendant code provider can drop a marker onto the base class or record if the Markers feature is enabled. See the “How to Enable” section of the following topic for more details: Markers.
Availability
Available when the caret is in a class/record name, reference, or its declaration.
Usage
Place the caret in the Logger class declaration for the code snippet below:
Press the Ctrl+. or Ctrl+~ shortcut to invoke the Code Actions menu, select Create Descendant from the menu, and press Enter.
If a base class contains abstract and virtual members that must be overridden in a descendant class, this code provider shows the “Members to override” window.
Select the members that you want this code provider to override in a descendant class and press Enter. The list does not include the methods that are already overridden.
In this example, select all members or click Select all in the “Members to override” window (see the screenshot above).
Note
Virtual members are not initially selected in the “Members to override” window.
After execution, this code provider creates a descendant class in a new file and opens this file. The descendant class contains a default constructor and overridden members for the selected abstract and virtual members. The new file name matches the descendant class name.
// LoggerDescendant.cs
public class LoggerDescendant : Logger
{
protected LoggerDescendant()
{
}
public override void GetMessage(int index) => throw new NotImplementedException();
public override void SendMessage(string message) => throw new NotImplementedException();
public override void SaveMessage(string message) => base.SaveMessage(message);
public override bool Equals(object obj) => base.Equals(obj);
public override int GetHashCode() => base.GetHashCode();
public override string ToString() => base.ToString();
}
// Program.cs
public abstract class Logger
{
public abstract void GetMessage(int index);
public abstract void SendMessage(string message);
public virtual void SaveMessage(string message)
{
}
}
Customization
Change Code Actions Settings
You can configure the Create Descendant code provider settings in the Editor | C# (Visual Basic) | Code Actions | Code Actions Settings options page.
For more information, refer to the following topic: Code Actions Settings.
For example, set the “Default body of newly-generated methods” setting to “Return default value with TODO comment”, as shown below:
Run the Create Descendant code provider. The following screencast shows the result:
Configure Expression Bodies for Overridden Members
The Create Descendant code provider can use expression body definitions when it creates a descendant class with overridden members (see the screencast above) according to the specified code style rule.
The “Use expression bodies” style setting is disabled in the Editor | C# | Programming Style page of the CodeRush options dialog:
Use the Visual Studio expression body preferences and the corresponding settings in the EditorConfig file to configure the expression bodies style in CodeRush:
Refer to the following topic for more information about the expression bodies style rule: Programming Style Rules.