My first post about this covered some really basic material. Here, I'm going to talk about a few features that I've recently learned about or started using. Remember, the more you can have your IDE do for you, the better you can spend your time on what's really important.
Code Snippets: This was a feature that I blatantly ignored for several years because I underestimated it. Everyday I find that it does more and more than I expected. Code Snippets are like smart code chunks where Visual Studio predicts what you want to do for that code block and can accurately fill in a lot of code for you. They're simple to use, too. As you type a keyword such as for, foreach, switch, and others you'll find that keyword in the IntelliSense. Just hit tab once to insert the keyword, and one more time to call on the code snippet. It'll write up the code block for that keyword and highlight variables you'll probably change. I.E. in a for statement, it'll let you name the initialized variable and the condition variable. If you update the initialized variable's name, it'll automatically update every reference to it in the proper scope! An example how it can be useful, we use enumerations a lot at work and for each one we eventually run it through a switch statement to decide what to do next. Let's say we had the following enum:
enum DrawStyle { Outline, Solid, None }
Writing out all the cases can get tedious, especially when there are a lot of enum values. What the switch code snippet will do is automatically fill out the following code:
switch (switch_on) {
default:
}
The variable switch_on will be highlighted and when you change to a variable of type DrawStyle, it'll automatically add in all the proper cases for the enum:
DrawStyle myDrawStyleValue = DrawStyle.Solid;
switch (myDrawStyleValue) {
case DrawStyle.Outline:
break;
case DrawStyle.Solid:
break;
case DrawStyle.None:
break;
default:
break;
}
It may take some time to break your usual habit to include this, but I can vouch that it's a great time saver and can be a valuable feature when you have a lot of code to write.
Code Generation: This little gem is a lot like code snippets but different. It allows you to begin using methods, classes or variables you haven't created yet. By that, I mean you can use classes in namespaces you haven't yet added to the top of the file and then have the namespace added based on the class you're trying to use. Let's say you were about to connect to a file with a FileStream. If you write FileStream fs; without already including System.IO then, as expected, you'd get an error under FileStream. If you right click FileStream then there will be a choice near the top that says Resolve. Under that menu you can have Visual Studio add the proper using or Imports statement to the file you're using, or you can, for that one reference, use the full reference path to the type. So far, it's just a simple feature to correct for a namespace you forgot, but Code Generation is in use all over Visual Studio. There are several situations where I'm writing a bit of code and I know I'm about to need a new method to pass some stuff to but I haven't written that method at all. If you just write the method the way you plan to use it, with all the parameters and assigning it's return value to a variable, then you can right click the method after the line is complete and find Generate under the context menu. Method Stub will probably be your choice most of the time, and it'll write the signature for that method in the proper scope with the proper parameters and return type. This method is just an inference and can edited, like if you wanted to add parameters with default values or you wanted the method to have a different scope. There are many different things that Resolve, Refactor, and Generate can all do from the context menu to save time and generate (or alter, in the case of Refactor) code that you would have written anyway.
Take some time to familiarize yourself with these features and any others you may find. Don't be afraid to wander through the menu bar or the tool bars to see what's available to you. Visual Studio is an awesome application that does so much. Regardless what IDE you use for what language you prefer, explorer it to make sure you're not working harder then you have to on things that could be done for you. You're bound to put out better code faster if you understand how to accurately use the tools available to you.