Events
May 19, 6 PM - May 23, 12 AM
Calling all developers, creators, and AI innovators to join us in Seattle @Microsoft Build May 19-22.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
typeof
to nameof
This refactoring applies to:
What: Lets you convert an instance of typeof(<QualifiedType>).Name
to nameof(<QualifiedType>)
in C# and an instance of GetType(<QualifiedType>).Name
to NameOf(<QualifiedType>)
in Visual Basic.
When: All instances of typeof(<QualifiedType>).Name
where someType
isn't a generic type. This exclusion is necessary because this case doesn't return the same string value as nameof(<QualifiedType>)
. The same is true for the Visual Basic instance.
Why: Using nameof
rather than the name of the type
avoids the reflection involved with retrieving a type
object, and is a more pragmatic way of writing it.
Place your cursor within the typeof(<QualifiedType>).Name
instance for C# or the GetType(<QualifiedType>).Name
in Visual Basic.
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Select from one of the following options:
C#
Select Convert 'typeof' to 'nameof':
Visual Basic
Select Convert 'GetType' to 'NameOf':
Events
May 19, 6 PM - May 23, 12 AM
Calling all developers, creators, and AI innovators to join us in Seattle @Microsoft Build May 19-22.
Register today