Ctrl Alt V for Variable
Nowadays it’s an open secret that refactoring is indispensible for creating clear, well-structured code. This change is reflected in the fact that in 2005, we saw several refactorings introduced in Visual Studio, including Rename, Extract Method, Encapsulate Field, and a couple of others.
However, this was not the first time that powerful and flexible refactorings were made available to C# developers. ReSharper’s rich set of refactorings, which keeps growing steadily from one release to the next, adds great value to Visual Studio.
Here is one of the sweet refactorings you’ll want to have at your disposal in C# and ASP.NET: Introduce Variable.
Introduce Variable lets you quickly create a variable from any expression. All usages of the expression are instantly replaced with the newly declared variable. Here’s a basic example:
- Select an expression from which to make a new variable.
- Press Ctrl + Alt + V to bring up the refactoring dialog, then provide the necessary details for the new variable. Select the “Replace all occurrences” option (occurrences are neatly highlighted in the editor as soon as this dialog opens) – if that’s what you want to do, of course.
- After you click Continue, the code looks like this:
Ok, so that’s how you can use the refactoring on an existing piece of code. Another way is to introduce a variable on the go. If you type an expression, and then it hits you that it should be declared as a variable (or a constant), just introduce it as one and keep typing.
Here’s a simplified example (if we forget for a second that we already have System.Math.PI available…)
With ReSharper you can introduce other things, too: Introduce Parameter and Introduce Field. More on that later.
P.S. Don’t mind the typo (that is, if you spotted one) – it will be fixed in the upcoming bugfix release.
Subscribe to Blog updates
Creating Custom AI Prompts
AI has swept through the software development industry like a wildfire. So people want to learn how to best use AI in their day to day tasks. In this post we’ll take a look at how to write custom prompts for use with the JetBrains AI Assistant in ReSharper and Rider so you can make the most of AI.&n…
12 Debugging Techniques In JetBrains Rider You Should Know About
Twelve must know debugging features in JetBrains Rider every developer should know.
Interceptors – Using C# 12 in Rider and ReSharper
Welcome to our series, where we take a closer look at the C# 12 language features and how ReSharper and Rider make it easy for you to adopt them in your codebase. If you haven’t yet, download the latest .NET 8 SDK and update your project files! In this series, we are looking at: Primary …