From the course: Visual Studio Developer Tips
Brace matching in C# - Visual Studio Tutorial
From the course: Visual Studio Developer Tips
Brace matching in C#
- [Instructor] For this first tip in the series, I want to look at some useful features that help when coding in curly-brace languages like C++, C#, and JavaScript. I'll start with the concept of brace matching. As you know, C# uses braces extensively. They delineate the boundaries of namespaces, class definitions, methods, enums, for loops, code blocks, and more. When working in code, it can be useful to quickly determine where a matching brace is located. There are a number of useful utilities that can help with this, like CodeRush and ReSharper. And there are some other code extensions available in Visual Studio. Today, however, I'm talking about what's included in the generic Visual Studio. I'll show you an example in this C# file. I'm editing the code, and I'm looking at the code on the bottom of this method. I'm interested in the curly braces on line 29 and 28. To activate the brace matching, I need to move my cursor to the curly brace. I can do that with my mouse, or I can use the keyboard. I'll use the keyboard first. I'll press the up arrow. And then I'll move to the right. When I'm on the outside edge of the curly brace, you see there is a gray rectangle that appears superimposed over the curly brace. That's the matching brace rectangle. If I press the space bar and move away from the edge of the curly brace, you'll see that that matching brace rectangle disappears. So I have to be right next to the curly brace, like this. Now if I look up on line 13, I can see that there's a curly brace up there. And it has the same matching rectangle. So that tells me that the curly braces on line 13 and 29 are a pair. Now, I said you could use your mouse too, so I'll use my mouse cursor and move to this curly brace on line 28. And you'll see that I get the matching brace on line 26. Now, when I move on the inside of this starting curly brace, the highlight disappears. That's because you have to be on the outside edge. So, with the starting curly brace, you need to be to the left of the curly brace. The curly brace matching rectangle is light gray, and I think that's hard to see. It's the default. It's subtle. And I like to enhance it a bit. So let me show you how to change those settings in the Visual Studios Tools Options window. So go to Environment and then Fonts and Colors. And then scroll down here to where you have this Brace Matching. Now, there's three items here. We're looking for this one here, this Brace Matching. And I will set the background color to an orange color. Now, you pick your own colors. I typically use a more subtle choice than I'm going to show you today. I'll dial this back to a light orange color, like about like that. Then I'll also change the item foreground to a dark red color. Now, when I return back to Visual Studio and do my brace matching, it's a lot more apparent which brace is matching because of the red color. And notice that the brace itself is still black. The dark red color that was referred to in the settings is the rectangle's outline. So that's your basics. That's brace matching. The next step is to talk about what happens when you want to move between braces. There's a keyboard shortcut you can use. You need to hold down the Control key and then tap the square brace key on your keyboard. Now, there's two square brace keys on your keyboard. We're talking about the right square brace. So hold down the Control key, tap the right square brace. And you'll see that my cursor moved to line 29. Now, do that again. Hold down Control, tap the same square brace key, and you'll see that it moves back up to line 13. So this is the real tip here, is not only can you see the matching brace, but you can move between them. Now, in this example I can see all my code on the same page, so it might not be that useful. But in a larger document, where I can't see the beginning of the method or the beginning of the statement, this is helpful. I also like to point out that the term brace doesn't just mean curly brace. Another type of brace, as far as Visual Studio is concerned, is the square brace. So if I move my cursor here, you'll see that I get the same brace matching highlight. And I can use the same keystroke to move between the beginning square brace and the ending square brace, like this. And also, this works for parentheses. Let me show you on this set of parentheses here. So I've got this if statement. I'm on the end parenthesis. And then I'll use the Control + square brace to move to the beginning of the parentheses. So the takeaway from this is, use the brace matching to find where your braces are. Change the colors to a more useful color if you find it too subtle. And the third thing is, you can move between your matching braces by using the keyboard shortcut.
Contents
-
-
-
Brace matching in C#4m 57s
-
(Locked)
Select vertical sections of code with Box Selection5m 50s
-
(Locked)
Move code with keyboard shortcuts2m 49s
-
(Locked)
Find duplicate code with the code clone analysis tool4m 8s
-
(Locked)
Paste JSON/XML as class3m 42s
-
(Locked)
Colorize output in the build window4m 37s
-
(Locked)
A better way to redock a tool window3m 57s
-
(Locked)
The Open Command Line extension3m 39s
-
Get consistent editor settings with EditorConfig3m 46s
-
(Locked)
Git at a glance: Updates to the Visual Studio status bar4m 1s
-
(Locked)
Surround a block of code with a code snippet2m 5s
-
(Locked)
Understand the document tab features3m 35s
-
(Locked)
Clipping to a border using an opacity mask5m 53s
-
(Locked)
Insert Line with Ctrl Enter2m 21s
-
(Locked)
Use the File Tab channel3m 31s
-
(Locked)
Use the Add New File extension4m 27s
-
(Locked)
Keyboard access for CodeLens4m 32s
-
(Locked)
Use the Clipboard Ring2m 56s
-
(Locked)
Use the GlyphFriend extension5m 41s
-
(Locked)
Activate Open File in Solution Explorer3m 14s
-
(Locked)
Use the enhanced scrollbar41s
-
(Locked)
Drag and drop in Solution Explorer5m 20s
-
(Locked)
Keyboard shortcuts for MVC views and controllers4m 17s
-
(Locked)
Pin data tips in debug window6m 6s
-
(Locked)
The Trailing Whitespace Visualizer extension4m 19s
-
(Locked)
Date rounding and other helpers5m 21s
-
(Locked)
Memorize window layout (Reuse)4m 26s
-
Use DialogResult6m 48s
-
(Locked)
Label a breakpoint2m 31s
-
(Locked)
The Image Optimizer extension2m 35s
-
(Locked)
Use Ctrl+Spacebar to fix case of variable names3m 40s
-
(Locked)
Use the nameof operator6m 21s
-
Discover untested code with code coverage7m 36s
-
(Locked)
Use the Hand tool in XAML designer2m 16s
-
(Locked)
Array Visualizer extension2m 35s
-
(Locked)
Use Ctrl+Tab to cycle through windows3m 18s
-
(Locked)
Understand line stroke patterns4m 37s
-
(Locked)
The Comment Remover extension3m 41s
-
(Locked)
Toolbox tip shortcuts6m 1s
-
(Locked)
Randomize data with LINQ4m 13s
-
Customize context menu4m
-
(Locked)
Get the file path1m 25s
-
(Locked)
Convert lists4m 34s
-
(Locked)
Enum flags5m 19s
-
(Locked)
Reference highlighting2m 4s
-
(Locked)
Evaluate performance with PerfTips4m 50s
-
(Locked)
Null propagation4m
-
(Locked)
Multiple editor views of same file2m 17s
-
(Locked)
Customize the output window7m 37s
-
(Locked)
Colored braces with the Viasfora extension4m 15s
-
(Locked)
Use the C# interactive window10m 47s
-
(Locked)
The Smart Semicolon extension3m 29s
-
(Locked)
Reference Highlighting1m 35s
-
(Locked)
IEnumerable Extension Methods4m 58s
-
Compare LiNQ sequences6m
-
(Locked)
The Clear Recent Project List extension3m 34s
-