Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn how to have visual studio generate code for you, suggest actions with Smart Tags, and even fix compiler errors automatically.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
At this point we know how to quickly and
easily navigate, edit, and
0:00
refactor our code with Visual Studio.
0:03
But we're going to go one step further and
have Visual Studio write some code for us.
0:05
This is called code generation.
0:10
In this method, I want to create a list
of strings, so I'll go ahead and
0:13
use a list of type string.
0:17
If you haven't seen a list before, a list
is a type of generic collection in C#.
0:19
When a type is generic, it means that
it's designed to handle any type.
0:23
So, we can declare a list and
put whatever we want into it.
0:29
In this example, I'm declaring my list and
telling it to expect a type of String.
0:32
For more information on generics,
check out the teacher's notes.
0:37
As you can see, for some reason I
don't have a list as an option, and
0:41
Visual Studio is letting me know that it
might be able to help with smart tags,
0:44
that light bulb to the left of my text.
0:48
To open a smart tag, I press Ctrl+ period.
0:50
Visual Studio has several options
available to fix this compiler error.
0:54
I can add a Using Directive,
which would get me generics.
0:58
I can use a fully qualified name, or
maybe I meant to type Select List.
1:01
In this case, I will have Visual Studio
add a Using Directive for me and
1:07
finish initializing my list.
1:10
I can now add strings to my list.
1:20
Next, for debugging purposes, I want
to send my list of strings to another
1:36
method to write them to the console.
1:40
I'll make a call to a method I am planning
on creating called WriteStringsToConsole.
1:43
By pressing Ctrl+ period,
1:55
I can have Visual Studio generate
a place holder method automatically.
1:57
In this next example, I've defined
an interface called Iloggable.
2:02
I want to support logging on
my new class LoggableType.
2:06
So I add that interface
to my class definition.
2:10
Visual Studio knows something is wrong
because my class says it implements
2:15
ILoggable, but
it doesn't actually implement it.
2:19
If I press Ctrl+ period on the interface
and select Implement Interface,
2:21
Visual Studio will implement
the interface for me with placeholders.
2:25
In this method, I'm calling
an external service to get a message.
2:33
The message this returns can't
be more than 140 characters, so
2:37
I'm trimming the string.
2:41
There's a bug in this code however,
2:43
because Substring will throw an exception
if message has fewer than 140 characters.
2:44
So I need to wrap that Substring
call in an if statement.
2:49
First, I select the text I
want to surround with if.
2:54
Next, I press Ctrl+K, Ctrl+S, and
I am given the surround with menu.
2:57
I will select if and give it my condition.
3:02
Surround with is handy, and
it works for other file types as well.
3:10
Give it a try in an HTML file or
an MVC view and see what happens.
3:14
Surround with uses a technique
called code snippets.
3:20
Snippets are an easy way to generate
code in your project quickly.
3:24
For example,
3:27
let me show you how to create a property
with a backing field using a snippet.
3:28
You can use the shortcut key Ctrl+K,
Ctrl+X to bring up the snippet menu, or
3:33
you can simply start typing the name
of the snippet if you know it.
3:37
In my case,
I know this snippet is called propfull.
3:40
So I will type in propfull and
press tab twice to insert the snippet.
3:44
My property is a string, so
I'll change the type and
3:52
press Tab to cycle through
the editable fields in the snippet.
3:55
Notice that as I change the type
to string on my backing field,
4:01
Visual Studio knows to change
it on the property as well.
4:05
Same with backing field name.
4:08
Once I'm satisfied with my snippet,
I'll press Enter and I'm done.
4:13
Code generation is a pretty broad topic.
4:18
There are tools out there like T for
Tool Box, again,
4:21
to advance code generation techniques and
you can create your own custom snippets.
4:23
See teacher's notes for further reading
if you're interested in digging deeper.
4:28
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up