Refactoring C# Help

Many developers develop their applications first for functionality and then, once the functionality is in place, they network their applications to make them more manageable and more readable. This is called refactoring. Refactoring is the process of reworking code for readability, performance, providing type safety, and lining applications up to better adhere to standard 00 (object-oriented) programming practices.

For this reason, the C# environment of Visual Studio 2008 now includes a set of refactoring tools. ‘You can find these tools under the Refactoring option in the Visual Studio menu. To show this in action, create a new class called Car in Visual Studio:

Now, suppose that in the idea of refactoring, you want to change the code a bit so that the color and the door variables are encapsulated into public .NET properties. The refactoring capabilities of Visual Studio 2008 allow you to simply right-click either of these properties in the document window and select Refactor ~ Encapsulate Field. This will pull up the Encapsulate Field dialog shown.

From this dialog, you can provide the name of the property and click the OK button. This will turn the selected public field into a private field, while also encapsulating the field into a public .NE:r property. After clicking OK, the code will have been reworked to the following (after redoing both fields);

As you can see, these wizards make it quite simple to refactor your code not just on one page but for an entire application. Also included are abilities to do the following:

  1. Rename method names, local variables, fields, and more
  2. Extract methods from a selection of code
  3. Extract interfaces based on a set of existing type members
  4. Promote local variables to parameters
  5. Rename or reorder parameters.

You will find the new refactoring abilities provided by Visual Studio 2008 a great way to get you the cleaner, more readable, better-structured code that you are looking for.

Posted on October 31, 2015 in Visual Studio 2008

Share the Story

Back to Top
Share This