coderushforroslyn-400838-refactoring-assistance-name-anonymous-type.md
Replaces a selected anonymous type with a newly-declared type.
Available when the cursor is in an anonymous type.
Configure the Name Anonymous Type refactoring in the Editor | C# (Visual Basic) | Code Actions | Name Anonymous Type Settings options page.
Switch to the code editor. Place the caret in an anonymous type, for example, as shown in the code snippet below:
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu. Select Name Anonymous Type from the menu and press Enter.
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: Person.vb
Public Class TestClass
Public Sub TestMethod()
Dim employee1 = New Employee1 With {
.id = 1,
.name = "Nick Johnson"
}
End Sub
End Class
//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);
}
}
}
'FileName: Employee1.vb
NotInheritable Class Employee1
Implements System.IEquatable(Of Employee1)
Property id As Integer
Property name As String
Overrides Function Equals(obj As Object) As Boolean
If TypeOf obj Is Employee1 Then
Return Equals(CType(obj, Employee1))
End If
Return MyBase.Equals(obj)
End Function
Overloads Function Equals(other As Employee1) As Boolean Implements System.IEquatable(Of Employee1).Equals
If ReferenceEquals(Nothing, other) Then
Return False
End If
If ReferenceEquals(Me, other) Then
Return True
End If
Return id.Equals(other.id) AndAlso Equals(name, other.name)
End Function
Overrides Function GetHashCode() As Integer
Dim hashCode = 47L
hashCode = CInt(((hashCode * 53) + id.GetHashCode()) And &H7FFFFFFFL)
If name IsNot Nothing Then
hashCode = CInt(((hashCode * 53) + EqualityComparer(Of String).Default.GetHashCode(name)) And &H7FFFFFFFL)
End If
Return CInt(hashCode)
End Function
Overrides Function ToString As String
Return String.Format("{{ id = {0}, name = {1} }}", id, name)
End Function
End Class