CHAPTER 3
The code editor in Visual Studio 2013 is one of the areas of the IDE where Microsoft made many investments. The goal is to make developers stay focused on the code they are writing, helping them perform common tasks more quickly and save time. This chapter describes new features in the code editor that will help you be more productive when writing code.
Peek Definition is a new feature that you can use to see and edit the definition of a class or class member inside a popup shown within the active code editor window. This helps you avoid the need to leave the active window in order to open the code file that contains the code block you need to edit. To understand how it works, create a new Console application then add a Person class like the following.
Visual C#
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public override string ToString() { return string.Format("{0} {1}, born {2}", this.FirstName, this.LastName, this.DateOfBirth.ToShortDateString()); } } |
Visual Basic
Public Class Person Public Property FirstName As String Public Property LastName As String Public Property DateOfBirth As Date Public Overrides Function ToString() As String Return String.Format("{0} {1}, born {2}", Me.FirstName, Me.LastName, Me.DateOfBirth.ToShortDateString()) End Function End Class |
The Main method of the sample application simply creates a new instance of the Person class and assigns some values as in the following code.
Visual C#
class Program { static void Main(string[] args) { Person person = new Person(); person.FirstName = "Alessandro"; person.LastName = "Del Sole"; person.DateOfBirth = new DateTime(1977, 5, 10); Console.WriteLine(person.ToString()); Console.ReadLine(); } } |
Visual Basic
Module Module1 Sub Main() Dim person As New Person person.FirstName = "Alessandro" person.LastName = "Del Sole" person.DateOfBirth = New DateTime(1977, 5, 10) Console.WriteLine(person.ToString()) Console.ReadLine() End Sub End Module |
Now suppose you want to make some edits to the Person class, such as renaming members or adding new ones. Right-click the type name (Person in our example) and select Peek Definition. As you can see from Figure 8, a pop-up appears showing the code of the Person class and the name of the code file where it is defined.

Peek Definition offers a fully functional editor, so you can change your class (or member) definition according to your needs without leaving the active window. If you make any changes, these are immediately visible in the code that uses the class. When done, you can simply click the usual Close button to hide the Peek Definition content. This tool is a very useful addition, because it not only allows you to stay in the active editor window while making changes to a type, but it also makes it easier to find type definitions among millions of lines of code and thousands of code files.
Note: This feature is available only in the Ultimate edition.
In many situations, you might need to know how many times an object has been used in your code and where. The previous (and current) versions of Visual Studio provide a tool called Find All References, which shows a list of references to an object inside a tool window called Find Symbols Results that you can invoke by right-clicking an object’s name in the code editor and then selecting Find All References. Visual Studio 2013 makes a step forward, offering an additional integrated view of code references called CodeLens. Take a look at Figure 9, which shows the Person class definition inside the code editor.

As you can see, above each type and member name Visual Studio shows the number of references. If you click that number, a tooltip will show where the object is used; if you pass the mouse pointer over the line of code that contains the reference, another tooltip will show the full code block containing the reference (see Figure 10).

CodeLens also shows the containing code file for each reference and the line number where the object is used, and allows fast navigation to the reference by double-clicking the line of code in the tooltip. Actually CodeLens does also an amazing job when your solution is under the source control of Team Foundation Server; in fact, it can show information about unit tests, passed tests, failed tests, and edits made by other team members.
In most of real world projects, code files are made of hundreds of lines of code, so finding specific code blocks inside a file can become difficult. In order to make it easier to browse very long code files, Visual Studio 2013 provides an improved scroll bar in the code editor window, known as enhanced scroll bar. Basically the scroll bar can show a map of the code (map mode) so that when you move the mouse pointer up and down, a magnifier shows a preview of the code block. This is very useful with long code files, if you want to see some code definition without jumping from one position to another in the code file.
To enable the map mode, right-click the scroll bar, then select Scroll Bar Options. In the Options dialog, locate the Behavior group and then select the Use map mode for vertical scroll bar option, as shown in Figure 11.

Note: Enabling the code preview is optional, but I encourage you to leave it selected. After all, it’s the real benefit of this tool.
You can also choose the size of the map by changing the value of the Source overview box. The default value is Medium, which is a good choice for most situations. Click OK to enable the map mode. When you go back to the code editor, you can see the scroll bar’s new look. Figure 12 shows an example.

When you move the mouse pointer over the scroll bar, a tooltip shows a preview of the code for the current position on the map. A blue line indicates the cursor position, yellow dots indicate edited lines of code, and red dots represent breakpoints. You can simply revert to the classic scroll bar by going back to the scroll bar options and selecting the Use bar mode for vertical scroll bar option.
Tip: The enhanced scroll bar works with all languages supported by Visual Studio 2013. This means that when you enable the map mode, the scroll bar will show the map for every code file in any language until you disable it again.
Another key feature of Visual Studio 2013 in the code editor is called Navigate To. With this feature, you can easily find the definition of a type or member by placing the cursor on the type or member and then pressing CTRL + , . Figure 13 demonstrates how Visual Studio 2013 shows types and members that contain the name selected in the code editor.

As you can see from Figure 13, a list of objects matching the type or member name is shown. You can press the up and down arrows on your keyboard to see every definition in the code editor without actually activating a window. As you can easily understand, Navigate To becomes particularly useful if you have multiple definitions of the same type in your solution and you want to go to its definition quickly.
The code editor in Visual Studio 2013 has been dramatically enhanced with new features that increase the developer’s productivity by making it easier to complete common tasks. Peek Definition, integrated references, the enhanced scroll bar, and Navigate To give their contribution to make the new IDE a great place for writing code.