In this post I will explain how to rename all the occurrences of an identifier using Visual Studio tool “Refactor Rename”
In some situation we need to rename all the occurrences of an identifier like a variable name, a method name, a property, a class name etc. In that situation it is not easy to find and replace all the occurrences of that identifier. For example, you have written a method and declared a variable named “con” for MySQLConnection.
Right click on the identifier and click Refactor -> Rename to open Rename window. You can also use the shortcut (Ctrl+R, R) after selecting the identifier to open the Rename window.
In the Rename window, enter new identifier name in the “New name” field and click OK. Optionally you can select three options below the Location. When first option is enabled, it will show all the occurrences of the identifier being renamed. When second and third option is enabled, it will enable renaming of identifier in the comments and strings respectively.
You can see preview of the identifiers being renamed in the Preview Changes dialog box as following. You can uncheck the identifier reference that you don’t want to rename. You can see location of each occurrences of the identifier in “Preview Code Changes” area by selecting an identifier in the top.
Now, you can see in the following code that MySqlConnection identifier “con” has been renamed to “mysqlcon”. All the occurrences of “con” in the whole project has been renamed to “mysqlcon” by just a few clicks