Skip to main content

Name Anonymous Type

  • 3 minutes to read

Replaces the anonymous type with a newly-declared type.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on an anonymous type.

Note

This refactoring is only available in .NET Framework 3.0 or higher (C# or VB).

#Notes

  • This refactoring automatically constructs the code for a class that matches the selected anonymous type. The anonymous type reference is replaced with the newly declared type’s constructor.

  • All anonymous types in the current project having the same shape will also be replaced by the new type.

#Example

class TestClass 
{
    public void TestMethod() 
    {
        var employee1 = new { id = 1, name = "Nick Johnson" };
    }
}
Public Class TestClass
    Public Sub TestMethod()
        Dim employee1 = New With {.id = 1, .name = "Nick Johnson"}
    End Sub
End Class

Result:

class TestClass {
    [DebuggerDisplay("\\{ id = {id}, name = {name} \\}")]
    private sealed class Employee1 : IEquatable<Employee1> {
        private readonly int _Id;
        private readonly string _Name;

        public Employee1(int id, string name) {
            _Id = id;
            _Name = name;
        }

        public override bool Equals(object obj) {
            if (obj is Employee1)
                return Equals((Employee1)obj);
            return false;
        }
        public bool Equals(Employee1 obj) {
            if (!EqualityComparer<int>.Default.Equals(_Id, obj._Id))
                return false;
            if (!EqualityComparer<string>.Default.Equals(_Name, obj._Name))
                return false;
            return true;
        }
        public override int GetHashCode() {
            int hash = 0;
            hash ^= EqualityComparer<int>.Default.GetHashCode(_Id);
            hash ^= EqualityComparer<string>.Default.GetHashCode(_Name);
            return hash;
        }
        public override string ToString() {
            return String.Format("{{ id = {0}, name = {1} }}", _Id, _Name);
        }

        public int id {
            get {
                return _Id;
            }
        }
        public string name {
            get {
                return _Name;
            }
        }
    }
    public void TestMethod() {
        var employee1 = new Employee1(1, "Nick Johnson");
    }
}
Public Class TestClass
    <DebuggerDisplay("\{ id = {id}, name = {name} \}")> _
    Private NotInheritable Class Employee1
        Private _id As Integer
        Private _name As String

        Public Sub New(ByVal id As Integer, ByVal name As String)
            _id = id
            _name = name
        End Sub

        Public Overrides Function ToString() As String
            Return String.Format("{{ id = {0}, name = {1} }}", _id, _name)
        End Function

        Public Property id() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property
        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
    End Class
    Public Sub TestMethod()
        Dim employee1 = New Employee1(1, "Nick Johnson")
    End Sub
End Class

#Animation

CSharpNameAnonymousType

See Also