ReSharper Web Help

The Convert Anonymous to Named Type refactoring converts anonymous types to nested or top-level named types in the scope of either the current method (locally) or the whole solution (globally). In the dialog box that this refactoring provides, you can also specify whether ReSharper should generate auto-properties or properties with backing fields, and opt to generate equality and formatting method overrides.

To convert an anonymous type to a named type
  1. Place the caret at either anonymous type initializer or new keyword.
  2. Do one of the following:
    • On the main menu, choose ReSharper | Refactor | Convert | Anonymous to Named Type.
    • Choose Refactor | Convert | Anonymous to Named Type on the context menu.
    • Press Ctrl+Shift+RCtrl+Shift+R, then click Anonymous to Named Type.
    The Replace Anonymous Type with Named Class dialog box opens:

    Convert_Anonymous_to_Named_Type__dialog_box

  3. Specify a name for a new class, and configure the following options:
    • Scope: ReSharper can convert anonymous types with compatible object initializers in the scope of either the current method or the whole solution.
    • Location: the new named type can be nested in the current type, or created as an independent entity.
    • How to generate properties: ReSharper can generate either read and write auto-properties, or read-only properties with backing fields in the new class.
    • How to generate methods: as an option, you can choose to create stubs for Equals(), GetHashCode(), and ToString() method overrides in the new class.
    • Show processed usages in 'Find Results': if this check box is selected, ReSharper shows the new named type as well as all its usages in the Find Results window
  4. Click Next.

Consider the example below:

Convert_Anonymous_to_Named_Type__before

The anonymous type is converted into the Declarations class:

Convert_Anonymous_to_Named_Type__after__declaration

All usages of the type found within the specified scope are modified accordingly:

Convert_Anonymous_to_Named_Type__after__usage

If you have selected Show processed usages in 'Find Results', the new class and any modified type usages are shown in the Find Results window:

Convert_Anonymous_to_Named_Type__Find_Results


This feature is supported in the following languages/technologies:

C# VB.NET HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts
feature_available

Tip

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