Web Design that taps into the haromny and vision of your dreams.

10 Visual Studio Tools You May Not Know About

By on in Coding

1,064 words, estimated reading time 5 minutes.

The Visual Studio editor has hundreds of features and tools for the developer. I'm going to list some of the tools that I find particularly useful, which most experienced developers I talk to don't even know about!

Encapsulate Field

As a good programming practice guideline, all public fields in a class should be encapsulated. This is easy to do thanks to the Refactor tools. Simply declare a private field, right click » Refactor » Encapsulate Field. This will automatically create a public property for you.

You will get the option to name the property and the tool will optionally update all references to the private field for you. You can also preview any changes before they are made.

Before:

private string _name;

After:

private string _name; 
public string Name
{
  get { return _name; }
  set { _name = value; }
}

Extract Method

If you find yourself rewriting duplicate code or copy and pasting the code from one class to another, you may find that you can save a lot of work by extracting that code into a method. You can use the Extract Method tool to create a method from the currently selected text.

Highlight a block of text, right click » Refactor » Extract Method. The selected text will then be converted into a method with a name you provide.

static void Main(string[] args)
{
  const int number = 45;
  string name = number.ToString() + " number text";
  Console.WriteLine(name);
}

Becomes:

static void Main(string[] args)
{
  const int number = 45;
  string name = MakeNumberText(int number)
  Console.WriteLine(name);
}
 
private string MakeNumberText(int number)
{
  string name = number.ToString() + " number text";
  return name;
}

Ok, so it's just a simple one line example, but you get the idea.

Surround With

Adding a try... catch... finally block to your code is as easy as selecting the code, right clicking and choose "Surround With...". This will pop up a shortlist where you can select tryf for a "try catch finally" or try for a "try-catch" Your selected code is now inside a try block and the catch block is now waiting for something to do!

You can also use the surround with tool which will add loops, namespaces, classes, while, using and many more blocks.

Easy!

Generate Method Stub

Your busy bashing out a new block of code and need to call an external method but don't want to stop. You already know the method name and it's parameters so just call it as you would normally.

string test = "this is a test: ";
 
for (int itemNo=0; itemNo<=10; itemNo++)
{
  Console.WriteLine(test + itemNo);
  float price = GetItemPrice(itemNo)
  Console.WriteLine(itemNo + " costs " + price);
}

Nothing wrong with this except that the GetItemPrice method does not exist. Rather than typing out a new method declaration, just generate a method stub and let the IDE do the work for you.

Right click the new method and select "Generate Method Stub" and the IDE will automatically insert the method for you:

private static float GetItemPrice(int itemNo)
{
  throw new Exception("The method or operation is not implemented.");
}

The exception will prevent you from forgetting to complete the method when you come to run the project.

I find this particularly useful when writing my business logic and UI layers. When writing my UI code I will call the logic layer:

int price = BusinessLogic.Products.GetItemPrice(itemNo);

Use the generate method stub and carry on working and finish the UI code - the code still builds and will compile normally. I can then use the go to definition shortcut to do the actual work in the GetItemPrice method at a later date.

Go To Definition

So you are buried deep into somebody else's code trying to figure out what everything is. You find a variable but don't know where it is declared. Or maybe there is a method call and you want to look into the code, or maybe you want to look at a class...

You can simply right click on any variable, class, method, property and select "Go To Definition" and the code editor will jump to the line of code where that object is declared. VERY useful in large projects with many files. You can use the "Navigate Backwards" button to return to where you were.

You can also use the Code Definition Window to see a read-only version of the relevant lines of code in a separate code pane.

Find All References

Another useful tool in a large project is the Find All References tool. This will allow you to find every line of code which uses the selected variable, method or class. Useful if you decide to remove or replace a class with a new version and need to see where the old one is used.

How is this different to find the text? Well, this tool will only look in the scope of the object, so if you are looking for a class member called Name it will only find references to this property in this class, not any other classes or properties with the same name. The tool will also not search within comments or strings which the find tool will.

Debug Exceptions Window

Tired of trying to remember a specific exception namespace or class, or trying to find a relevant exception to throw? You can use the Debug » Exceptions window (Ctrl+Alt+E) to see a structured list of all the default system exceptions. You can drill down into the Common Runtime exceptions, into System and view all the exception classes.

Comment/Uncomment Sections

You can quickly comment out large quantities of text by using the comment section tool. This can be accessed from the toolbar or the Edit » Advanced menu. You can also use the Ctrk+K, Ctrl+C shortcut. To uncomment multiple lines you can use Ctrl+K, Ctrl+U.

You can also use the multi line comment as well if you wish...

Locate Matching Brackets

A little keyboard shortcut Ctrl+] will cause the caret to jump between matching brackets or parentheses and can be invaluable for locating missing or additional brackets.

Vertical Selection

You can use the mouse to select letters and lines in a horizontal manner, but did you know that you can use the Alt key to select columns? This is great if you need to clear a load of characters or text in a vertical column rather than in rows. Simply press and hold down the Alt key, then start clicking and dragging with the mouse.

Have you got any tips or shortcuts to share with our readers? Please let us know by leaving a comment on this article.

Last updated on: Wednesday 21st June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.