Skip to main content

Name Anonymous Type

  • 4 minutes to read

Purpose

Replaces a selected anonymous type with a newly-declared type.

Availability

Available when the cursor is in an anonymous type.

Notes

  • This refactoring constructs the code for a class that matches the selected anonymous type.
  • This refactoring scans the entire project and replaces all matching anonymous types with the new class.

How to Use

  1. Configure the Name Anonymous Type refactoring in the Editor | C# (Visual Basic) | Code Actions | Name Anonymous Type Settings options page.

    Settings

    For example, choose Include the [DebuggerDisplay] attribute, Generate Equals() and GetHashCode() methods, and Generate the ToString() method options. Click Apply and OK to save the changes and close the options page.

  2. Switch to the code editor. Place the caret in an anonymous type, for example, as shown in the code snippet below:

    Note

    The blinking cursor shows the caret’s position where the Refactoring is available.

    public class TestClass {
        public void TestMethod() {
            var employee1 =  new { id = 1, name = "Nick Johnson" };
        }
    }
    
  3. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu. Select Name Anonymous Type from the menu and press Enter.

    Invoke

    The Name Anonymous Type refactoring replaces the anonymous type with a newly-declared type, creates a new source file named after this type (IEmployee1.cs, in this example), and moves the declared type to that file. This refactoring also runs the Rename refactoring.

    Invoke

    Note

    You can configure whether CodeRush opens a generated file when it applies this refactoring. CodeRush also allows you to change the position of the newly-generated type in code (the new type can be placed above or below the active type). Refer to the Code Actions Settings topic for more information.

  4. In the code editor, you can type a new name for the generated type or leave it as is. Press Enter in the code editor or click Apply in the Rename hint to apply the changes and close the hint.

The following code shows the result:

//FileName: Person.cs
using System;

namespace MyApp {
    public class TestClass {
        public void TestMethod() {
            var employee1 = new IEmployee1 { id = 1, name = "Nick Johnson" };
        }
    }
}
//FileName: IEmployee1.cs

using System;

namespace MyApp
{
    [System.Diagnostics.DebuggerDisplay("\\{ id = {id}, name = {name} \\}")]
    sealed class IEmployee1
    {
        public int id { get; set; }
        public string name { get; set; }

        public override bool Equals(object obj)
        {
            if (obj is IEmployee1)
            {
                return Equals((IEmployee1)obj);
            }

            return base.Equals(obj);
        }

        public bool Equals(IEmployee1 other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return id.Equals(other.id) && Equals(name, other.name);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hashCode = 47;
                hashCode = (hashCode * 53) ^ id.GetHashCode();
                if (name != null)
                {
                    hashCode = (hashCode * 53) ^ System.Collections.Generic.EqualityComparer<string>.Default.GetHashCode(name);
                }

                return hashCode;
            }
        }

        public override string ToString()
        {
            return String.Format("{{ id = {0}, name = {1} }}", id, name);
        }
    }
}