Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As soon as you have more than a couple of pages of code in a project, you'll want to use the Visual Studio navigation features to move intelligently through your code. Let me show you how that's done. I'm going to open up Visual Studio and load this NavigatingCSharp project, and then I'm going to open these two C# files: Book.cs and Program.cs. The first thing I want to talk to you about is this outlining feature. This is turned on by default, and what happens in Visual Studio in say a C# application is that every subsection of your code--like this function here--has an outline automatically added to it.
So I can click on this Minus sign here, and collapse that down temporarily, so I don't have to look at the code that's inside that section. You might notice there are three dots over here. If I hover over those three dots, I'll get a preview of what's inside there. So I don't have to open it up just to look at the code that's in there. Another way of creating sections of code is to use what's called a region. So what I can do is I can come up here and type in "#region", and then some string, "hide this code", and then on line 12, I'll add a #endregion--all one word, and you see what happened over here in the outliner? It added a new outlining section.
So now I can collapse my custom-made section, and then my custom comment shows up here for the header of that collapsed section. This is very handy for hiding code that you're not using anymore. It's also used to put in things like all your events for a class can go inside a collapsed section called events, and then you know how to find them later. To navigate through your code, there are a couple of techniques you can use. I'm going to switch over to the Book class temporarily. I'm looking at this code in this function UpdatePrice, and there are a number of curly braces in here.
There is a clue that I get from Visual Studio about matching curly braces when I put my cursor in front of this top curly brace. If you look down on line 21, you'll see that the curly brace down there has a gray outline on it. That's how Visual Studio tells me that these two braces are related. But I can also navigate down to that curly brace by holding down the Ctrl key and tapping the right square bracket. Notice how my cursor moved down the line 21. Now, I'll do Ctrl+Right Bracket, and it moves back up to line 12.
Very handy when you have big code files and you want to quickly move to the bottom of the code section. When you're looking at code, there are a multiple ways that you can view separate views of the code. So I've got this Book class, and I want to compare it with another file, say this Program.cs file. Right now, I can compare them, but I have to keep toggling back and forth. What I can do is I can pick one of these tabs up and drag it out and place it on the side or the bottom of the page. Now I've got this little helper in the center of the window that's showing me-- right now it's going to dock to the left.
And I want to move it down here, so it's docking to the bottom. Now I've got two separate tab sections, and I can have multiple tabs down here in this bottom tab section and multiple tabs in the top tab section, and then I can compare the text. When I'm done, I can grab this tab and slide it back up and push it back into the top section. I can also do the same thing with multiple views of this book class. There are two methods I can use. One is I can go to the Window menu and choose New Window.
And since my Book tab is selected, it will create another copy of the book tab. And then I can use the technique I just showed you to grab this. And this time I'll move it over and slide it side by side, and now I've got two separate views of the same physical file. The other way of splitting the view is to use this little button over here on the top of the scrollbar, the one that has the two arrows on it. If I grab that and slide it down, I'm splitting the view. I still have one tab, but I have two views. To unsplit it, I can take this and either slide it up, or the fast way to do it is to double-click on this black line.
Next, I want to show you how to navigate to name sections of your code. I have this class called Book. It has a function called UpdatePrice. It also has a function down here called OtherMethod. So I'm going to go over here to this Program file, and I'm going to show you that I am calling UpdatePrice here, and I'm instantiating the Book class here. So what I can do is put my cursor on Book and then press F12 to navigate over to that Book class. If I am on the method and I press F12, then it will navigate over to the method.
See, now I am on line 11. If I choose a class that I didn't write, like this Console.WriteLine, and I press F12, then what Visual Studio does is it opens up a text file that contains the structure of this Microsoft class. It shows me I was working with this public static void WriteLine method. Also, there is something called reference highlight. So if I select this newPrice variable-- it's actually a parameter coming into this function--then notice that after about a one-second delay, that line 13 and line 15 light up and show me that I'm using this parameter in those two sections of my code.
This also works if I click on this variable up here. After about a one-second delay I see them using it on line 15, and I'm also using it on line 25--a nice way of visualizing the sections of your code To continue on with this mode, I'm going to go back to the Program file, and this time I'm going to right- click on this UpdatePrice method. I'm going to use this Find All References. What that does is open a separate window and show me those three places in my code that have references to UpdatePrice. The first one, if I double-click on it, is the place where the function is declared.
The second one, it shows me where I'm calling it here on line 11, and when I double-click of this one, it shows I'm calling it on line 18. I can also do something called View Call Hierarchy, and this is a slightly different view. Now I can see all the calls that are coming into UpdatePrice. There is a call coming from AnotherDemo and one from the Main function. And I can also see that within that this method, somewhere within my UpdatePrice function, I am calling the OtherMethod, which is a method that I wrote, and I'm also calling the WriteLine function that Microsoft created in their console class.
Finally, if none of these navigation features are making you happy, we can use our own custom bookmarks. I can place little tokens in my code and then use those as navigation aids. First, let me close these two windows anyway. I'm going to close the Call Hierarchy window, and I'll close the Find Symbol Results window by clicking on the X. And then I'm going to drop a bookmark. I'm going to move my cursor right here on line 9, and then I'm going to use the bookmark keystroke, which is Ctrl+K, and then I press Ctrl+K a second time. And if you look on the very left edge of the screen, you'll see that there is now a gray and blue rectangle over there. That's my bookmark.
I put a couple more in. I put one here, Ctrl+K, Ctrl+K, and then I'll switch over to my Book class, and put one here, Ctrl+K, Ctrl+K. And now here comes the power. I can use those bookmarks I've dropped in there, and I can come up to this Bookmark toolbar and click back or forward. So I'll click Back. It will then cycle me through each of my bookmarks, and those keep going around and around. This is really handy when you have a big project and you're currently working on three or four files and you're working in separate areas and you want to remember where you are.
So you put these temporary bookmarks in. They affect your copy of the project, not any other developer's copy. And when you're done, you can just use the same keystroke--Ctrl+K, Ctrl+K--to turn off the bookmarks. As you can see, there are many ways to navigate your code besides using the traditional up down arrows and mouse scrollbars. Why don't you try a few of these new techniques on your next project?
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97083 Viewers
61 Video lessons · 84399 Viewers
71 Video lessons · 68609 Viewers
56 Video lessons · 101095 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.