Visual Studio: Regular expression to apply NameOf operator

Microsoft Visual StudioLeave a Comment on Visual Studio: Regular expression to apply NameOf operator

Visual Studio: Regular expression to apply NameOf operator

The introduction of the nameof operator in C# and NameOf in Visual Basic.NET has made it easier to refactor large sections of code. If you have existing code with manually supplied variable names as string literals, the values should be updated with the new operator to improve the readability. A nameof expression in C# outputs the name of a variable, type, or member as a string constant. The expression is evaluated at compile time and has no effect at run time.

Below we have listed two common exceptions that use the name of an input parameter as their first input argument; ArgumentNullException and ArgumentOutOfRangeException. The constructors of both exceptions accepts a parameter name as the first argument.

ArgumentNullException

In this example we will use capturing groups to extract parts of the regular expression match. The captured value, the parameter name string literal, is then replaced with the nameof expression.

To find all references to an ArgumentNullException press Edit > Find and Replace > Replace in Files on the main menu bar. You can also use the keyboard shortcut Ctrl+Shift+H to open the same dialog.

Regular expressions to apply the nameof operator in Visual Studio 2020.
Regular expressions to find and apply the nameof operator in Visual Studio 2020.

When the “Find and Replace” dialog is displayed, enter the following regular expressions in the first two text fields:

Find what expression:
ArgumentNullException("(.*?)");

Replace with expression:
ArgumentNullException(nameof($1));

You also need to make sure that the “Use Regular Expression” check box is checked. Otherwise the search will not succeed as expected.

In the “Find what” expression we use the capturing group(.*?)“. The regular expression will match any string literal within the two double quotes. In our case this would be the first parameter of the expression, i.e. the name of the argument. In regular expressions you place a sub-expression in parentheses, and then the captured value is stored in the regex variable $1. The variables $1 through $99 are single-digit and double-digit backreferences supported by C# and Visual Basic .NET.

Every set of capturing parentheses from left to right, as you read the pattern in a regex pattern, is assigned a number, regardless of whether or not the engine employs these parentheses while evaluating the match. This left-to-right numbering is strict.

ArgumentOutOfRangeException

We can perform the same find and replace operation for the ArgumentOutOfRangeException. This exception also expects the first argument to be the name of a method parameter.

Find what expression:
ArgumentOutOfRangeException("(.*?)");

Replace with expression:
ArgumentOutOfRangeException(nameof($1));

Remember to check the “Use Regular Expressions” option.

Regular expressions to correct the ArgumentOtOfRangeException.
Regular expressions to correct the ArgumentOutOfRangeException.

In this article we have showed how to replace string literals with the nameof expression. In both cases the the first argument of the exceptions was the parameter name. For the more general ArgumentException the second argument is the parameter name, while the first argument is the error description. You can use a similar approach in this case, but you need to use two capture groups to find the correct match.

Related

Ulf Emsoy has long working experience in project management, software development and supply chain management.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top