ReSharper 2024.2 Help

Convert Anonymous to Named Type refactoring

Anonymous types are very convenient when you need to process a result of a query locally. However, if you need to pass the result around your program or if there are several queries that return the similar name/value parts, you may probably need a named type.

In such cases, ReSharper can convert the existing usage (or usages) of an anonymous type into a named type and update the usages. If necessary, ReSharper will find and convert similar anonymous types in the whole solution. In the dialog that this refactoring provides, you can customize the created type — specify whether it should have auto-properties or properties with backing fields, or generate equality and formatting method overrides.

In the example below, two similar anonymous types are converted into the BookLIst class, though we only invoke the refactoring on one of them. All usages of the types found within the specified scope are modified accordingly:

class MyTest { void Test(List<Book> books) { var bookList = from book in books select new {book.Author, book.Title}; } } class MyNewTest { void Foo(List<Book> library) { var bookCatalog = from item in library select new {item.Author, item.Title}; } }
public class BookList { public string Author { get; set; } public string Title { get; set; } } class MyTest { private void Test(List<Book> books) { var bookList = from book in books select new BookList {Author = book.Author, Title = book.Title}; } } class MyNewTest { private void Foo(List<Book> library) { var bookCatalog = from item in library select new BookList {Author = item.Author, Title = item.Title}; } }

Note that this refactoring cannot be applied if the anonymous type initializer has another anonymous initializers inside. For example:

var bookList = from book in books select new {book.Author, title = new {book.Title}};

Here you can apply this refactoring to new {book.Title} but not to new {book.Author, title = new {book.Title}}.

Convert anonymous type(s) into a named type

  1. Place the caret at either anonymous type initializer or at the new keyword.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Replace Anonymous Type with Named Class.

    • Right-click and choose Refactor | Replace Anonymous Type with Named Class from the context menu.

    • Choose ReSharper | Refactor | Convert | Anonymous to Named Type… from the main menu.

    The Replace Anonymous Type with Named Class dialog will open.

  3. Specify a name for a new class, and configure the following options:

    • Scope: ReSharper can find anonymous types with compatible object initializers (similar name/value parts) in the whole solution, or you can apply the refactoring to the current method only.

    • Location: the new named type can be created as a nested class in the current type, or created aa an independent class in the current namespace.

    • 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 checkbox is selected, ReSharper shows all anonymous types that it finds and converts in the Find Results window. You may want to tick this checkbox if you are converting similar anonymous types in the whole solution.

  4. To apply the refactoring, click Next.

  5. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

Replace Anonymous Type with Named Class

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

Results of converting anonymous types into a named class

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is not available in Visual Basic

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

Last modified: 23 September 2024