coderushforroslyn-115601-refactoring-assistance-use-name-of.md
Converts a string literal to the nameof expression with the corresponding variable/member as an argument. The string literal should contain a variable or class member name.
You can use this refactoring instead of rename tools to keep your code valid when you rename definitions. This refactoring is better than the rename tools since these tools do not search for variable/member names within string literals.
Available when the caret is in a string literal that contains a variable/member name.
Place the caret in a string literal that contains a variable or member name.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Use NameOf from the menu.
After execution, this refactoring changes the string literal to the nameof expression with the corresponding variable/member as an argument.
public void AddRecord(object data1, object data2, object data3) {
if (data1 == null)
throw new ArgumentNullException(nameof(data1));
//...
}
Public Sub AddRecord(ByVal data1 As Object,
ByVal data2 As Object,
ByVal data3 As Object)
If data1 Is Nothing Then
Throw New ArgumentNullException(NameOf(data1))
End If
'...
End Sub
Note
This feature is available as a part of Code Cleanup.
See Also