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
Let's start the course by running a simple C# program!
- If you're watching this video on the Treehouse site, there should be a "Launch Workspace" button on the page. Click it!
- You'll see a dialog where you can change the name of your new workspace, or you can just keep the default.
- Click the "Launch it!" button when you're ready.
- A new window will open with a Treehouse Workspace. Give it a minute to load.
- When it launches, you'll see a sidebar on the left with several files, including a
Program.cs
file. - All the other files in the project are just there to support compiling and running our code. The only file we're interested in is this
Program.cs
file, which contains all the code for our program. - Click on the file, and it'll load in the text editor on the right.
class Program
{
static void Main(string[] args)
{
// Displays "Hello World!" on the terminal.
System.Console.WriteLine("Hello World!");
}
}
- There's a lot going on in these 8 lines of code, and if you haven't programmed before then there's some parts that you won't understand. But that's totally okay!
- For now, all you need to know is that most C# programs start out with code just like this, with a line that says
class
, a line that saysstatic void Main(string[] args)
, and so on. - You don't even have to remember this syntax, because C# comes with tools that can generate starter code like this for you.
- For now, all you need to know is that most C# programs start out with code just like this, with a line that says
- For right now, the only part we're interested in is the code between these curly braces following
static void Main
.- This is the code that gets run when your program starts.
- The first line here isn't even part of the program code. Lines that start with two slashes like this are called comments, and they're ignored by C#. They're only there for developers like you and me to read.
- The second line displays the text "Hello World!" on the screen.
- That's all there is to it! This program is ready to run. So, let's run it!
- Click in the Console area at the bottom. You'll know it's been activated if a blinking cursor appears down there.
- The main way to run C# code is using a framework called .NET Core.
- Type
dotnet
, a space, and the wordrun
.
- That will find and run our program, which prints out the message from our code.
- You can also click in the editor and change the message if you want.
- Save your work by clicking the File menu and choosing Save.
- Then click in the console again and re-run the program.
- When you're done, you can close the window. Don't worry, your changes will be saved on the site's Workspaces page.
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
[MUSIC]
0:00
Hi I'm Jay McGavren, a web developer and
teacher at Treehouse.
0:09
And I'm here to introduce you
to the C# programming language.
0:10
We'll get into a detailed
explanation of how C# works shortly.
0:15
But that explanation will be a lot clearer
if you've already tried a C# program.
0:19
So let's run our first program now.
0:24
If you're watching this
video on the Treehouse site,
0:27
there should be a Launch Workspace
button on the page, click it.
0:29
You'll see a dialogue where you can
change the name of your new workspace or
0:33
you can just keep the default.
0:36
Click the Launch it
button when you're ready.
0:41
A new window will open with
a Treehouse Workspace.
0:46
Give it a minute to load.
0:49
When it launches you'll see a sidebar on
the left with several files including
0:52
a Program.cs file.
0:56
All the other files in the project
are just there to support compiling and
0:59
running our code.
1:02
The only file we're interested in is this
Program.cs which contains all the code for
1:03
our program.
1:07
Click on the file and it'll load
in the text editor on the right.
1:09
There's a lot going on in
these eight lines of code.
1:13
If you haven't programmed before,
1:16
then there's some parts that you won't
understand,f but that's totally okay.
1:17
For now, all you need to know is that
most C# programs start out with code
1:21
just like this,
with a line that says class,
1:25
a line that says static void main,
etc., and so on.
1:28
You don't even have to remember this
syntax because C# comes with tools
1:31
that can generate starter
code like this for you.
1:36
For right now the only part
we're interested in is
1:39
the code between these curly
braces following static void main.
1:42
This is the code that gets
run when your program starts.
1:46
The first line here isn't even
part of the program code.
1:50
Lines that start with two slashes
like this are called comments and
1:53
they're ignored by C#.
1:57
They're only there for
developers like you and me to read.
1:58
The second line displays the text
Hello World on the screen.
2:02
That's all there is to it.
2:05
This program is ready to run,
so let's run it.
2:07
Click in the console area at the bottom.
2:10
You'll know it's been activated if
a blinking cursor appears down there.
2:12
Type dotnet, a space, and the word run.
2:18
That will find and run our program, which
prints out the message from our code.
2:23
You can also click in the editor and
change the message if you want.
2:30
So I'm gonna change Hello World
to my name, Hello Jay.
2:34
Save your work by clicking in
the File menu and choosing Save.
2:37
Then you can click in the console again
and rerun the program, dotnet space run.
2:41
When you're done,
you can close the window.
2:50
Don't worry, your changes will be
saved on the site's workspace's page.
2:52
So that's what a simple C#
program looks like, but
2:56
C# can do a lot more than
just say hello world.
2:59
It's used to build popular products that
you may have used yourself at some point.
3:02
There's Stack Overflow,
a website where people ask and
3:06
answer software development questions.
3:09
The Unity game engine supports
building games in C#,
3:11
including hits like Subnautica.
3:14
The Visual Studio IDE,
3:17
which many developers use to write
C# code is itself written in C#.
3:19
And these are just a few examples.
3:24
If you want to write software that runs
fast, but it's also easy to maintain,
3:25
C# is a great programming
language to choose.
3:30
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