HomeSearch

Visual Studio: Go To Definition, Find All References

This tutorial covers the Visual Studio integrated development environment. It focuses on the C# language.
Visual Studio. This 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.
Features. To start, let's look at some of the features of Visual Studio. Most are available in recent versions of Visual Studio and Visual Studio Community. Visual Studio provides tools for you to automatically refactor your code.DebuggingActivation RecordEncapsulate FieldTODO Comments

Snippets: These allow you to type a few letters and then press tab twice to insert a common fragment of code.

Snippets

Macros: These can be useful for formatting XML files and also controlling build options for unmanaged programs.

Visual Studio 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.
Code example: 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.
Code you can find all references on: C# 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.

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.

Tip: My suggestion is that you always use "Remove and Sort" on a file that you have finished writing.

And: If you are starting a new source code file, you can just leave the usings in any order.

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; } }
IntelliSense. 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. Now, the Main method by default receives an args array of strings. Try typing "args" and then press the period key.Console.WriteLine

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.

Info: Why are items like AsQueryable, Average, and Cast in the list? These 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
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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls