Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn how Visual Studio helps you write code.
-
0:00
Now let's look at how to write code in Visual Studio.
-
0:03
Here in the Editor window I have a number of files open.
-
0:06
I can rearrange these tabs by dragging them around.
-
0:10
Program.cs is where I want to spend most of my time right now so
-
0:13
I always want to be able to find it, even if I open a bunch of other files.
-
0:17
That happens a lot when I'm tracking down a bug or exploring code.
-
0:21
So you can pin any of these tabs by right-clicking the pin button here.
-
0:26
This moves the tab to the far left side and keeps it there.
-
0:30
I don't need these other files right now.
-
0:31
I can close all except the ones I'm interested in by right-clicking
-
0:35
on the tab I want and clicking Close All But This.
-
0:39
Notice here that there is also an option to close all but the tabs I've pinned.
-
0:44
If you ever want to know where the file you are working on is located
-
0:47
in the file explorer, you can mouse over it, this shows you the full path.
-
0:52
You can also right-click on the tab and
-
0:54
click Copy Full Path which copies the path of the file to the Windows clipboard.
-
0:59
Or you can click Open Containing Folder, which opens the file explorer and
-
1:04
selects the file.
-
1:06
All these things are very handy when working with files.
-
1:11
Okay, now that I've cleaned up my workspace a little,
-
1:13
let's take a look at some of the features of the text editor.
-
1:17
One of the first things you notice is the coloring.
-
1:20
Of course, the colors you use will be different if you're using a different
-
1:23
color theme.
-
1:24
The colored text is what is called syntax highlighting.
-
1:27
Comments are in green, language keywords are blue, type names
-
1:33
that are not keywords are in light blue, and strings, like this one, are in red.
-
1:39
Here you'll notice that these using namespace statements are grayed out.
-
1:43
That's because there isn't any code in this file using those namespaces.
-
1:47
Visual Studio is telling us that we can remove these lines.
-
1:51
The colored line on the left shows which lines have been changed since the last
-
1:55
time the file was saved.
-
1:56
If I save the file, everything will turn green.
-
1:59
There's also a lot of information here in the scroll bar.
-
2:03
This solid blue line shows where in the file the cursor is located at the moment.
-
2:07
Here we see a green square.
-
2:09
This shows where we have compiler warnings in the code.
-
2:13
They correspond to where we see green squiggles under the text here.
-
2:17
Mousing over the green squiggles shows a tool tip with the warning.
-
2:21
This one is telling us that the variable greeting is assigned but not used.
-
2:26
Warnings are important to pay attention to because they often mean
-
2:29
there's something we've forgotten to do and there's potentially a bug in the code.
-
2:34
Errors show up in red.
-
2:36
These are things that will stop the code from building all together.
-
2:39
If I delete this closing parenthesis,
-
2:42
you will see a red squiggly line where the parenthesis once was.
-
2:45
We also see a little red square show up here in the tool bar.
-
2:49
This feature is constantly making us aware of compiler errors, and warnings,
-
2:53
saves us a lot of time when it comes time to compile and run the code.
-
2:57
In fact, I find that I encounter compiler errors when building my
-
3:01
code a lot less often now.
-
3:03
Visual Studio tries to reduce typing required to write code.
-
3:07
It has a number of ways to do this, for instance,
-
3:09
if you start typing a list of keywords or other suggestions will appear.
-
3:14
It selects the one you are most likely to use.
-
3:16
With the symbol selected,
-
3:17
just hit Tab to have Visual Studio finish typing it for you.
-
3:21
As I keep typing, I get to where I want to write a string literal.
-
3:25
Notice when I type the first double quote,
-
3:27
it automatically adds the ending double quote.
-
3:29
I can keep typing my string.
-
3:34
When I get to the end of the string, I can hit Tab, and
-
3:36
it moves the cursor to the end of the line, so I can type the semicolon.
-
3:40
I'll show some more of these auto complete features while I write another
-
3:43
method here.
-
3:46
I'll type S, T, A, Tab,
-
3:51
Space, S, T, Tab, the name of the method.
-
3:57
And open parenthesis.
-
3:59
See the closing parenthesis was added automatically?
-
4:03
S, T, Tab, and then a variable name, and Tab again to get to the end of the line.
-
4:09
Return, and then an open curly brace.
-
4:12
See the closing curly brace was automatically added?
-
4:15
Hit Return again and the second curly brace is moved down two lines, and
-
4:19
the cursor is indented.
-
4:22
As you can see, typing in Visual Studio's
-
4:24
text editor is very different than typing in a regular word processing program.
-
4:29
It uses its knowledge of the symbols in your program, conventions used in C#,
-
4:34
and the C# syntax and grammar to help you speed up your coding.
-
4:38
For some coders, this may take a bit to get used to.
-
4:41
After a while, it become second nature and you don't even think a lot about it.
-
4:45
This allows you to spend your time thinking about the problem you're trying
-
4:48
to solve and less about things like spelling syntax and what things are named.
-
4:52
Another way Visual Studio tries to help you
-
4:55
while typing is a feature called IntelliSense.
-
4:58
IntelliSense helps you when you type a dot at the end of a valid expression.
-
5:03
It determines what the type of the expression to the left of the dot is and
-
5:07
then shows you a list of methods, properties and
-
5:09
fields that you can call from the scope you are in.
-
5:13
As you start typing,
-
5:14
it highlights the symbol that you are most likely looking for.
-
5:17
You can also use the up and down arrow keys to select the name.
-
5:21
When you hit Tab, period, Space or any other character that makes sense in that
-
5:25
context, it types the rest of the name for you.
-
5:28
If you don't want it to type what is selected,
-
5:30
just hit the Escape key on your keyboard.
-
5:33
There are lots of other ways Visual Studio helps with typing code
-
5:36
including the use of snippets and code generators
-
5:39
which are more advanced than we need to cover here in this workshop.
-
5:43
And of course, if you don't like some of these code typing features,
-
5:45
you can always turn them off.
-
5:47
However, I encourage you to experiment with them for
-
5:50
a while before you make that decision.
You need to sign up for Treehouse in order to download course files.
Sign up