ReSharper 2021.1 Help

Generate Formatting Members

ReSharper | Edit | Generate Code | Formatting Members
Alt+Insert | Formatting Members
ReSharper_GenerateFormattingMembers

Any type in .NET implements the ToString() method, which returns a string representation of an object of the type. To return a meaningful string for our types, we often need to override the ToString() method.

ReSharper allows you to automate this routine with the Generate formatting members command.
Depending on the target C# version, ReSharper either uses interpolated string or String.Format(). Note that you can always convert between those with context actions Alt+Enter.

In the example below, this command is used to generate the ToString() method based on _radius and _center fields.

Before generationAfter generation
class Circle { int _radius; Point _center; }
class Circle { int _radius; Point _center; public override string ToString() { return $"{nameof(_radius)}: {_radius}, {nameof(_center)}: {_center}"; } }

Generate overrides for formatting members

  1. In the editor, set the caret at the type name or within a type at the line where you want to insert overrides for formatting members. If the caret is on the type name, the generated code will be added in the beginning of the type declaration.

  2. Press Alt+Insert or choose ReSharper | Edit | Generate Code… from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.

  3. In the Generate popup, select Formatting Members.

  4. In the Generate dialog that appears, select fields and/or auto-properties to be used in the ToString() override.
    Generating 'ToString()' with ReSharper

    If there are no fields/properties in your type or you do not select any of them, ReSharper, depending on your settings, throws new NotImplementedException(), returns default value, or puts code that will not compile in the body of the generated methods. You can configure the settings on the Code Editing | Members Generation page of ReSharper options.

    Optionally, use the following controls in the dialog:

    • ToString already exists — appears if an implementation of ToString() already exists and lets you choose whether to:
      • Replace the method if it already exists.

      • Put the newly generated method side by side with the existing one.

      • Skip generating a new method altogether.

    • Use 'nameof' — appears if the target C# version is higher than 6.0. If this checkbox is selected, ReSharper will use nameof(symbol_name) to define the string representation of the symbol, otherwise, it will calculate the string representation automatically, for example, a field named _radius will be rendered as "Radius".

  5. Click Finish to complete the wizard.
    You can also click Options to review or modify common code generation preferences on the Code Editing | Members Generation page of ReSharper options.

You can also generate the override by choosing Overriding Members in the Generate menu, but in this case the override will return base.ToString().

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C#Feature is available in Visual Basic .NET Feature is not available in C++ Feature is not available in HTML Feature is not available in ASP.NET Feature is not available in Razor Feature is not available in JavaScript Feature is not available in TypeScript Feature is not available in CSS Feature is not available in XML Feature is not available in XAML Feature is not available in Resource files Feature is not available in build script files Feature is not available in Protobuf Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 08 March 2021