Hello World: Your First C# Program
Crash Course
- Start a new C# Console Application from Visual Studio's start page by choosing Create a new project, choosing the Console App template, and naming your project.
- Inside of the Main method, you can add code to write out stuff using a statement like Console.WriteLine("Hello World!");
- Compile and run your program with F5 or Ctrl + F5
- The template code includes code that does the following:
- using statements pull in other chunks of previously written code (like #include in C++ or import in Java).
- The namespace block puts all of the contained code in a single collection.
- The code we are writing goes in the Program class, in a method called Main which the C# compiler recognizes as the start point for the application.
Introduction
In this tutorial, we'll make our very first C# program. Our first program needs to be one that simply prints out some variation of "Hello World!" or we'll make the programming gods mad. (It's tradition to make your first program print out a simple message like this whenever you learn a new language. It is usually really simple yet still gives you enough to see the basics of how the programming language works. Also, it gives you a chance to compile and run your program, with very little chance of introducing bugs.)
So that's where we'll start—creating a new project and adding in the single line of code we need to write "Hello World". Once we've got that, we'll compile and run it, and you'll have your very first program!
After that, we'll take a minute and look at the code that you have written for your program in more detail before moving on to slightly more difficult but infinitely more awesome stuff in future tutorials.
Creating a New Project
OK, let's get started with our first C# program! Open up Visual Studio.
When the program opens, you will see the Start Page, as shown below.
On this screen, choose Create a new project on the bottom right side of the window.
The next step is to pick a template. The one you want to use is called Console App, but be sure to get Console App and not Console App (.NET Framework), since that is the old one, and you want the new one.
If you're having a hard time finding it among the crowd of templates, you can use the dropdowns at the top to filter down to the C# language and the Console project type. (If you still can't find it after this, you might want to try re-running the installer to make sure you installed the right components, as instructed in the previous tutorial.)
Once you've selected the Console App template, press Next to move on.
The next step is to configure your project the way you want it. Give it a name (I've chosen "Hello World"). You can pick a location, though the default is fine. Just take note of where it is so you can find it later, since it isn't in an obvious spot. You can check the box for "Place solution and project in the same directory" if you want. I usually do for small programs to keep them simpler.
Once configured, press Next.
On the final page, you can choose the framework version and whether to use top-level statements.
You will typically want to use the highest-numbered version you have installed. As of the time of writing, that's .NET 8, but it will change over time. (If you are reading this and the latest version is .NET 9 or later, please let me know so I can update it.)
The "Do not use top-level statements" checkbox requires a bit of explanation. There are two ways to make a C# program: the easy way and the old way. The old way has a whole lot of boilerplate code hanging around that clutters things up and distracts from your program. But it is also more explicit about what's going on, with less guessing on the programmer's part. C# programmers used the old way for 20 years and got comfortable with it, and some C# programmers don't love the new way. (For people who love technology, we still collectively aren't all that great at dealing with change.)
If you're going through this tutorial set, I'm going to encourage you to use the new way because it is just so much simpler to work with. However, if you want, you can check this box and figure things out the old way. (In the short term, it just means everything goes in the Main method, so it shouldn't be too hard to figure out.)
In short, for most people, I recommend just leaving these settings as they are.
Either way, once you're done, press Create to get your project made.
A Brief Tour of Visual Studio
Once your project has loaded, it is worth at least a real brief discussion of what you see before you. For starters, what you see should look something like the image below:
Yours make look a little different, but it should be close.
On the left side is your main editor window. It is a tabbed window, so you can have multiple files open in different tabs and change between them. It will be a few tutorials before we're using more than one file. This is where you'll write code. You'll spend 95% of your time here.
At the top of the window is the main menu, with your typical options for things like saving, closing, and opening files and doing different types of edits, and getting help. Below that is the main toolbar, which has a handful of buttons that do common tasks like saving your files, etc. We'll use both the menu and the toolbar a fair bit as we go.
On the right side is a panel containing several other windows. On the top is the Solution Explorer. This gives you a high-level view of everything that composes your program (referred to as a "solution"). If you look carefully, you can see that there's a Program.cs in the tree view. Files that end with .cs are C# source code files. The template made exactly one of these to start, but we can add others later. (If you don't see a text file open in the editor space on the left, double-click Program.cs to open it.) You'll use the Solution Explorer whenever you need to manage things about your project or add or remove parts of your project. You'll spend maybe 3% of your time over here.
On the bottom right is the Properties window. When you select things in the UI, especially things in the Solution Explorer, this Properties window will show you properties for the selected thing and give the chance to edit them. You'll spend maybe 1% of your time here, and you could close it out if you want. (It can always be re-opened by clicking on View > Properties Window.
There are other windows that can either be opened by request, or that will open when you do specific actions in Visual Studio, and you'll spend a bit of time in each of these as you program.
There's a lot here, but you don't have to master it all right away. It can be learned over time.
Running your Program
As it turns out, the code in the template has already made a working "Hello World" program for us, so we're ready to go. We just need to tell the computer to compile our code, which converts it from C# source code into binary instructions that the computer can run, and then run our program.
There are many ways to do this:
1. Go to Debug > Start Debugging on the main menu.
2. Pressing F5.
3. Pressing the green triangular arrow button on the main toolbar.
Do any of the above to run your program. You should see something like this:
That's it! You did it! Your first C# program!
Modifying Your Project
The next task is a homework assignment: change the text to say something besides "Hello, World!".
Then run your program again to see it say your new text.
How the Code Works
Now that we've got our program running, let's talk briefly about what this code does. While you may have modified your text, the program originally looked something like this:
// See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!");
The first line, which starts with //, is a comment. We'll discuss comments in more depth in the next tutorial, but this is text for the programmer that the computer ignores. You can delete this line if you want because this particular comment is actually a message from the people who made the template to you Once you've read it, there's no need to keep it.
That leaves us with a single important line: Console.WriteLine("Hello, World!");. There is a lot we can learn even from this single line.
First, this whole line is called a statement. Each statement is a command to do something specific. In C#, you build a program by making a series of statements, and the computer runs them from top to bottom and left to right, the same way that you write plain old text in human English. Most statement types are typically ended with a semicolon (;) mostly because the period or dot symbol (.) is used for other things.
The Console and WriteLine things are both identifiers, or in more casual conversation, simply as names. They refer to some named element that exists out there in your program. As we'll soon see, we can make our own names for things, but there are also a bunch of things that come for free. Console and WriteLine are two that are free. These come from a giant pile of support code that your programs can leverage, called the Base Class Library, and are available in all C# programs.
These named elements are also hierarchical—some things contain other things. The . operator, called the dot operator or the member access operator and often just read aloud as "dot" allows you to dig down from one element to one of its children. So out there in the world is a thing called Console that has a member called WriteLine, and the full Console.WriteLine accesses it.
Specifically, Console is a type of thing called a class. We'll deal with a lot of classes as we go, and we'll make our own in due time. But you can think of a class as a single thing that performs some job in the system. It carries with it all of the data needed to do its job and a collection of tasks you can ask it to do. These tasks are called methods, and WriteLine is one such method.
So Console is a class with the job of interacting with the console window. One task you can ask it to do is write a line of text into the console window, which is done by using the WriteLine method. Asking an object to run a method is done by putting parentheses after the name of the method and supplying any needed information between the parentheses.
The WriteLine method needs one piece of information, which is the text to display. In this starter code, that is the text "Hello, World!". By simply changing this text, we can supply alternate text to display. Or in other words, the WriteLine method can display any text we supply, making it flexible.
The text in double-quotes is interpreted by C# as text, regardless of its contents. This is a type of programming element called a literal, because it represents a value exactly as it is shown, rather than something for the computer to evaluate. To get more technical, in the programming world, text is usually referred to as strings. So this is a particular flavor of literal called a string literal. We'll see how to do literals for other types like numbers later on. All of the "simple" data types in C# support making values with literals, but more complex types will require special code to make them. But we'll get into those details later.
Altogether, this one line is instructing the computer to find the thing named Console, digging down to find its member called WriteLine, which is a method, and then telling that method to run with a specific string.
This is not the only kind of statement that exists in C#, and we'll learn other types later, but this is not an uncommon type of statement either.
Now a challenge for you: can you make a program that displays multiple lines of text?
What's Next?
Whew. That was kind of a long tutorial, I know. And we haven't even gotten a whole lot done yet. Just writing out some simple text.
That's OK, though. We have to start somewhere, and this is where everyone starts.
Before taking our first huge step into the world of programming, we'll first take a small pit stop and take a look at a very important feature of the C# language (and also of virtually every other programming language out there) called comments. This will show us how to make notes for ourselves and other programmers about what the program is supposed to do.
I think you should tell everyone about the ";" at the end of each statement you write.
Thanks for the feedback, Alphonse!
For those that don't know, C# is a C based language. That being the case, each statement is terminated (sent to the interpreter for compilation) with the ';' operator. It should be the last thing to appear on each code line.
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets
Hi.
This question is kinda stupid. But I can't find the "Console Application" in my templates?
I had this problem too. However I'm guessing that under the templates tab you choose .NET and there you will see console project. Hope that helps.
This post has example using old VS, in new VS, File->New -> Project -> Select ".NET" from left pane and Console app from right pane
.NET developer, online tools : https://www.minify-beautify.com
Tech blog: https://developingdaily.com
Hello,
This is not such a big deal, but in my case at least, compiling and running the program in release mode also makes it terminate by default after printing ‘Hello World!’. Maybe there was some change in convention for the console since the time this tutorial was written (as I guess the behaviour is system-dependent)?
(This happened to me when I followed this tutorial, and it crossed my mind once again as I am currently looking at the XNA-MonoGame ‘Console Windows’ tutorial!)
Hi,
None of the images show in this tutorial.
Trying to open the images as a new tab displays:
The file does not exist.
Love the site, but missing the images such as "StartPage.png", etc. on this page "Hello World: Your First C# Program"
Post preview:
Close preview