>> Hello everybody and thank you so
much for joining me here today.
My name is Cody Beyer,
Program Manager on the Visual Studio
for Mac team here at Microsoft.
Today, we're going to talk about Snippets.
Snippets are a feature of
the IDE that allow you to quickly add blocks of
code to your source without having to type in the entire block.
For example, if you want to add an if statement, simply type IF,
hit "Tab" twice and if statement will
expand allowing you to fill in the details from there.
I'm going to take a look at this source file right here.
As I mentioned, if statements are one of
the many powerful snippets available in VS for Mac.
Others include; for loops,
foreach loops, and while statements.
As you can see with each of these,
when I type in the snippet and hit "Tab" twice,
I am provided cursors
and the areas where I need to add information.
For example, here I have int i,
i is less than max, and i++.
Within these areas, I'm able to type
in what variables I need to use here.
For example, if I need i to be less than 20,
all I need to do is type in 20,
and this gives me the for loop that I require.
Another powerful aspect of this feature is that you can add
custom snippets that match what your team needs.
To do this, open up Visual Studio,
Preferences and navigate to
the Snippets section which you can find under the Text Editor.
Here you're given a powerful editor to add or edit
snippets that are available. Let's first add one.
To do so, simply click
"Add" and you're provided with a snippets add screen.
Here you can type it in
details of you're snippets such as a shortcut you wanted
to use as well as what type of file the snippet will be used in.
So I'm going to add a snippet that says Hello World.
So my shortcuts are going to be hello.
The group is going to be C#.
The description is going to be Hello World,
and the mime is going to be text C#.
Here, I can type in the template text.
For example, Console.Writeline ( "Hello, world!")
Now, I'm going to click "Okay" to save this snippet.
Let's go back to a source file and see if that snippet works.
Now that we've added this snippet,
you can execute it by typing in
the keyword that you set when you created it.
In our case, keyword was hello.
So by typing in hello and hitting "Tab" twice,
that expands out to include Hello World.
You can also edit snippets in Visual Studio for Mac to either
make them more specific to your needs or more powerful.
To do this, again open
the Preferences dialogue and navigate to the Code Snippet section.
I'm going to edit another custom snippet that I made
previously on using statements.
To do this, I'm going to click
"Edit" and again I'm given a code editing dialogue.
You may notice some differences in
this statement from the Hello World one we made previously.
The biggest standout is the use of variables.
Here you can see the strings surrounded by
dollar signs represent variables
that you must change when you use the snippet.
For example, the value variable here must
be the name of the item which you're going to be using.
One thing I've noticed is that I'm missing the ending
parenthesis within my new value section.
So I'm going to add these parentheses here,
and simply click "Okay" and again click "Okay" to save it.
Now, that we've edited our snippet, let's see if it works.
I'm going to type in using and tab again.
Perfect, my Snippet expands and provides
me areas where I can type in all the things I needed to change.
Now, I'm going to make a using statement using a list.
So I'm going to type in list string.
As you can see, not only did
the left side of the declaration change,
but so did the right side as those are both
the same variable when I created this snippet.
So now I'm going to the notset and say name, my list.
So I'm going to hop into the notset section
and name my list cats, and as you can see,
it not only changed the name of the declaration,
but also change the name or
the value within the using statement itself.
Let's see again how we did that.
So open up Preferences and go to the using section.
You can see that I used two variables: value and name.
By using the same variable in multiple places,
I ensure any changes I make to one is reflected in the other.
If you expand this out further,
you can make very powerful snippets that can
really make it easier to reuse specific blocks of code.
Today, we learn not only how to
use existing snippets in Visual Studio for Mac,
but also how to create
your own snippets that can be used in a customer environment.
To learn more about Visual Studio for Mac,
please visit aka.ms/vsmacdocs or
stay tuned for the next video in our series. Thank you.