You are on page 1of 7

10 Visual Studio Tools You May Not Know About

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 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 popup a short list 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 to 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 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 text? Well this tool will only look in the objects scope, 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.

.Net Command Line Tools


Visual Studio provides a set of command line tools to software engineers to accomplish some tasks that are either not available in the IDE or tools that can be run in batch. Some of these tools are essential for distributing applications over the Internet. Listed below are the main tools and essential tools, together with a description and example usage. This is not a comprehensive or definitive list of the tools.

Csc.exe C Sharp Compiler


Command line compiler tool compiles .cs files to .exe. Compile to mySource.exe with a target of release:

csc.exe mySource.cs

Compile to mySource.exe with a target of debug:

csc.exe mySource.cs /debug

Compile to Demo.exe:

csc.exe mySource.cs /out:Demo.exe

Compile to mySource.dll:

csc.exe mySource.cs /target:library

Compiles all cs files to exe:

csc.exe *.cs

Display full list of command line parameters:

csc.exe -?

Gacutil.exe Global Assembly Cache Tool


Command line tool to inspect the current machine's global assembly cache. You can view, install or uninstall assemblies from the GAC with this tool. List all assemblies currently installed on the machine:

gacutil.exe /l

Install an assembly into the GAC:

gacutil.exe /i myfile.dll

Uninstall an assembly from the GAC:

gacutil.exe /u myfile.dll

Display full list of command line parameters:

gacutil.exe

Ildasm.exe Intermediate Language Dissembler


Not strictly a command line tool, but not installed onto the start menu. This program will provide a disassembly of a file provided that it is a Portable Executable (PE) executable (generated by the ILASM tool or C# compiler). The tool will inform you of any references the program has, the classes, methods and data objects in the executable and other debug information.

Ilasm Intermediate Language Assembler


Compile Intermediate Language to Portable Executable (PE) Compiles to source.exe:

ilasm.exe source.il

Compiles to source.dll

ilasm.exe source.il /dll

compiles to source.exe using keyfilename to strongly signed private key: ilasm.exe source.il /key:keyfilename

Sn.exe Strong Name Tool


Helps sign assemblies with strong names. It provides options for key management, signature generation and signature verification. Generate a random key and store it in mykey.snk:

sn -k mykey.snk

Verify an assembly:

sn -v mykey.snk

aspnet_regiis.exe ASP.NET IIS Registration Tool


This tool is used to install, or register, ASP.Net with the IIS server.

aspnet_regiis -i

You might also like