Skip to main content

Make Explicit (and Name Anonymous Type)

  • 3 minutes to read

Converts an implicitly-typed local variable to a variable with an explicit type, creates a named type to represent the expression on the right, and replaces the anonymous type with a newly-declared type.

#Availability

From context menu or via shortcuts:

  • when the caret is on an implicit variable declaration (the variable does not have a type explicitly declared and it is initialized with a complex-type value).

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 complex type assigned to the selected variable. The declaration then explicitly specifies the variable type and creates the assigned value via that type’s constructor.

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

  • This refactoring combines Name Anonymous Type and Make Explicit.

#Example

public object TestMethod(int i, string EmpName)
{
    var employee = new { Id = i, Name = EmpName };
    return employee;
}
Public Function TestMethod(ByVal i As Integer, ByVal EmpName As String) As Object
    Dim employee = New With {.Id = i, .Name = EmpName}
    Return employee
End Function

Result:

[DebuggerDisplay("\\{ Id = {Id}, Name = {Name} \\}")]
private sealed class Employee : IEquatable<Employee>
{
    private readonly int _Id;
    private readonly string _Name;
    public Employee(int id, string name)
    {
        _Id = id;
        _Name = name;
    }
    public override bool Equals(object obj)
    {
        if (obj is Employee)
            return Equals((Employee)obj);
        return false;
    }
    public bool Equals(Employee obj)
    {
        if (obj == null)
            return false;
        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 object TestMethod(int i, string EmpName)
{
    Employee employee = new Employee(i, EmpName);
    return employee;
}
<DebuggerDisplay("\{ Id = {Id}, Name = {Name} \}")> _
Private NotInheritable Class Employee
    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 Function TestMethod(ByVal i As Integer, ByVal EmpName As String) As Object
    Dim employee As Employee = New Employee(i, EmpName)
    Return employee
End Function

#Screenshot

rsMakeExplicitAndNameAnonimousType

See Also