Home » Did you know » Renaming Properties, Methods and Variables

Renaming Properties, Methods and Variables

B03B0065 Have you ever written some code and named something one thing only to realize that it should be named something else?

If you haven’t you haven’t been programming for very long.  Maybe you started a new job and you spent two weeks writing code using your previous employer’s naming conventions only to find out that this employer expects things to be named slightly differently.

So now you not only have the item named incorrectly, but you’ve referenced it from other files, so it isn’t a simple matter of just giving it a new name.  No, the code that is referencing it has to be changed as well.  So what are you going to do?

In the “old days” we’d change the name, recompile and fix all the references.  But now there is a much easier way to fix this problem.  All you need to do is to right-click on the property, method, or variable and select “Refactor” > “Rename…” from the context menu.

This will bring up a dialog box where you can give the item a new name and optionally select if you’d like to preview the changes, make changes in your comments (that are also referencing this variable name) and search for it in strings.

Press “OK” to continue and all of the names of the item as well as all of the references to that item will automatically be changed for you.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • C# Properties Get and SetC# Properties Get and Set My son is learning to program.  Last week he asked me to explain C# properties get and set and, as it turns out, it looks like many others are asking for the same.  So, I’ve decided […]
  • Easily Find Classes, Methods, and VariablesEasily Find Classes, Methods, and Variables There is a bit of a "trick" that I use routinely in Visual Studio to help me find the definition of Classes, Methods, and Variables in my solution regardless.This "trick" works in […]
  • Readonly variables in CSharp? Really?!Readonly variables in CSharp? Really?! I'm sure most of you are familiar with the keyword "const," which allows you to declare a variable and give it a value and assures that no other code will change the value. const int v = […]
  • CSharp’s Property ShortcutsCSharp’s Property Shortcuts There are a lot of nice shortcuts in the CSharp language that most of us never use.  But if you take the time to learn them, you can be as productive as a student I had who had learned all […]
  • CSharp VAR MisconceptionsCSharp VAR Misconceptions I find it amazing that .NET 3.5 has been out for over a year and people still don’t understand the “var” keyword. Just last week I got a comment on one of my videos asking why I used […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

2 Pingbacks/Trackbacks

  • Giuseppe Turitto

    I use the Refactor option when I have to do Copy Paste of some previous code on a new project, sometimes because it requires some extremely close functionality, but with slight changes that can’t be decoupled enough in order to make it a generic code, sometimes for “productivity” (the Boss want’s that new screen in one hour, and if you can make it in half it will be better, and you know it for fact that to do it right it requires an hour and ten minutes. So old Copy and Paste works fantastic, but you end up with bunch of method name’s that are not really related to the new code. So Refactor and voila instant code in matter of minutes. Boss Happy, saved the Bonus, Wife Happy.

  • Pingback: Dew Drop - November 25, 2008 | Alvin Ashcraft's Morning Dew()

  • Pingback: Dew Drop – November 14, 2013 (#1667) | Morning Dew()