TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Visual Studio Tips and Examples

This tutorial covers the Visual Studio integrated development environment. It focuses on the C# language in VS.

Visual Studio helps us develop C# programs.

A program is text. It can be edited in any text editor. In Visual Studio we use features that make programs easier to create. IntelliSense completes statements.

Tip: To reduce the headaches of C# programming, and .NET development, Visual Studio is useful.

Features. To start, let's look at some of the features of Visual Studio. Most are available in Visual Studio Professional 2008, 2010 and 2013, as well as Visual Studio Express. Visual Studio 2005 also has much of the same functionality.

DebuggingActivation RecordEncapsulate FieldTODO Comments

Tip: Visual Studio provides tools for you to automatically refactor your code. These features can be at times useful.

Other features. We next present features of Visual Studio that do not primarily involve the C# language. They can be useful for formatting XML files and also controlling build options for unmanaged programs.

Tab OptionsPost-Build, Pre-Build Macros

Go To Definition. Large projects are hard to navigate. It is difficult to locate a method definition. The Go To Definition feature in Visual Studio makes this easier. It locates the method definition for you and shows it right in the editor.

This simple example uses two files. The first file, Program.cs, uses a class Something located in Something.cs. Right-click on the Secret() method call in Visual Studio. The editor will show the Secret.cs file where Secret() is located.

Program: C#

class Program
{
    static void Main()
    {
	Something something = new Something();
	something.Secret();
    }
}

Code shown after Go To Definition: C#

class Something
{
    public void Secret()
    {
    }
}

For this simple project, the feature is not useful. The feature becomes much more useful for large projects. Many files are often open at once. Go To Definition will focus the correct one.

This feature will work correctly even if the method call is invalid. If you try to call Secret with an argument, this will cause a compile-time error. Go To Definition will still locate Secret() in this case.

Compile-Time Error

Note: Go To Definition is a useful feature in Visual Studio. It is often used along with Find All References.

And: These search and find features make complicated and large projects much easier to handle.

Find All References. Program can become very complex and huge. One of the best features in Visual Studio 2010 for alleviating this problem is the Find All References command. This feature provides an instant search command.

This is the program shown in the screenshot above. The "A" method is called in two places and it is defined in one place. When you right-click on "A" and select Find All References, Visual Studio locates two references and one definition.

C# program that you can find all references on

class Program
{
    static void Main()
    {
	A();
	B();
    }

    static void A()
    {
    }

    static void B()
    {
	A();
    }
}

References to A

C:\...\Program.cs - (15, 9) : A();
C:\...\Program.cs - (5, 9) : A();
C:\...\Program.cs - (9, 17) : static void A()

In the Find Symbol Results pane, you can also right-click on an entry and select Go To Reference or Go To Definition depending on what type of result it is. This makes navigating projects faster.

Tip: To see if a method is unused, use Find All References—if no references other than the definition exist, you can probably delete it.

Also: Find All References can be used on type names such as string or Dictionary. Custom identifiers are also searched.

Comment Selection. You can use Visual Studio to automatically comment out one or more lines of code in your C# program. This is done with the Comment Selection option. You can then use Uncomment Selection to remove the comment characters.

Let's begin by looking a simple C# program. It's not the most useful program, but it serves its purpose here. Try selecting the two lines in the Main method and right-clicking your mouse. Click on "Comment Selection" in the context menu.

First example: C#

class Program
{
    static void Main()
    {
	int value = 1;
	value *= 2;
    }
}

Second example: C#

class Program
{
    static void Main()
    {
	//int value = 1;
	//value *= 2;
    }
}

Uncomment Selection. Next, we can try the Uncomment Selection context menu item. This command will remove a "//" sequence from the start of every line that has one. If no "//" sequences are found, it does nothing.

You can use Comment Selection on the same text multiple times. Multiple "//" sequences are added. Uncomment Selection only removes a single "//" sequence each time it is called. This information is current for Visual Studio 2010.

Tip: Comment Selection and Uncomment Selection are useful options in Visual Studio, but only if you know they are there.

Also: I prefer my "//" comments to have a space following them, but this is not important to the compiler or to many other programmers.

Comment

Organize Usings. In your C# program, you have a list of using directives at the top that allow you to easily access types. These may be in any order, but to make reading them easier, alphabetizing them is important.

To use the Organize Usings feature, right-click anywhere on the using directives and select Organize Usings. In these examples, we see the initial program. We then see the program with Remove Unused Usings (and Remove and Sort) invoked.

Initial program: C#

using System.Linq;
using System.Text;
using System.Collections.Generic;
using System;

class Program
{
    static void Main()
    {
	StringBuilder builder = new StringBuilder("Welcome");
	Int32 length = builder.Length;
    }
}

Selected, Remove Unused Usings: C#

using System.Text;
using System;

class Program
{
    static void Main()
    {
	StringBuilder builder = new StringBuilder("Welcome");
	Int32 length = builder.Length;
    }
}

Selected, Remove and Sort: C#

using System;
using System.Text;

class Program
{
    static void Main()
    {
	StringBuilder builder = new StringBuilder("Welcome");
	Int32 length = builder.Length;
    }
}

My suggestion is that you always use "Remove and Sort" on a file that you have finished writing. If you are starting a new source code file, you can just leave the usings in any order.

Tip: Organize Usings gives you the ability to clean up your source code, making it easier to understand.

IntelliSense makes coding C# programs easier. But what does it mean to use IntelliSense in Visual Studio? This article is intended for beginners to Visual Studio and the C# language—we look at the basics of IntelliSense.

First, make a new console program in Visual Studio. You will get a console program similar to the one shown below. Now, the Main method by default receives an args array of strings. Try typing "args" and then press the period key.

Console

And: IntelliSense will activate—a popup with all the possible ways to complete your statement will appear.

Console program: C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
	static void Main(string[] args)
	{
	    args.
	}
    }
}

Next, you can scroll through all the possible members (methods and fields and properties) that can complete your statement. Double-click one or press enter or tab on the desired statement.

Why are those items in the list? You might be wondering why args has items like AsQueryable, Average, and Cast in the IntelliSense list. These items are extension methods from the System.Linq namespace.

LINQ

Note: Try removing the "using System.Linq" directive from the top of your program and then see how IntelliSense changes.

Settings.settings. For Windows Forms programs, you can use Visual Studio to store data persistently throughout application executions. This feature involves the Settings.settings file and is described here.

Settings.settings

Extern alias. The extern alias directive allows you to specify a physical location of a class in addition to its name. This helps when you have two class libraries that introduce a class with the same name.

Extern AliasUsing Alias

Snippets. One neat feature in the Visual Studio IDE is snippets. These allow you to type a few letters and then press tab twice to insert a common fragment of code. They eliminate repetitive statements and reduce overuse injuries with your hands.

Tip: A useful snippet is the prop snippet. It's a personal favorite of mine. I haven't yet told prop about my feelings for it.

Snippets

Summary. Visual Studio not only provides a way to write C# and VB.NET programs, but it has many features that make writing these programs significantly easier. It is worthwhile to spend some effort to improve your skills using Visual Studio.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf