In Part 1 of the Introduction to C# series, we create our first program in C#. We will write a Console Application and learn about some basic Input/Output through the console.
If you haven’t yet installed Visual Studio, please see my tutorial on Installing Visual Studio, then come back here. Thanks!
Those of you who have learned a new programming language before have probably seen the Hello World example. And some of you may be rolling your eyes, thinking, “Oh great, another Hello World program. This is a waste of my time!”
Maybe it is, but hopefully not. I have found that its almost universal use for the first step in a new language really helps establish some familiarity when learning a second programming language. I think Hello World is a great place to start. I’m not trying to blow your minds, I’m trying to teach you C#. So if there are some programmers out there who just need a primer in the syntax of C# and don’t want to precisely follow the Hello World model, feel free to play around with Visual Studio. You *probably* won’t break anything irreparably. Maybe you could create a dialog between two dogs. Do something creative. The language is your oyster.
For the rest of us, let’s start simple. Launch Visual Studio just like you would any other program on your computer. Got it? Good.
First Time Setup
If you have done nothing with Visual Studio beyond what we covered in the installation tutorial, you may be asked to sign in to Visual Studio. You really don’t have to do this. If you have a Microsoft account already, you can go ahead and login, but if not, I would just skip it. Click Not Now.
Next you will be asked to choose a theme. I recommend the default theme.
Create a New C# Program
After you click New Project, you will see a window that look like this:
In this window, enter “Hello World” into the Name field. It will automatically fill in the Solution Name for you. Now, double click “Console Application.” Now, Visual Studio does a bit of loading before bringing up the default code for a Console Application. It looks like this:
Congratulations! You’ve created a new Console Application project. In the next section, we’ll start to write some C# code.
Writing to the Console
Don’t worry about all the default code for now. Just understand that:
- A class holds the code that your project will run
- A namespace groups classes together so that they can see each other
- The Main method is the first method that any program runs
So, we need to put the code we want to run inside the Main method.
In a Console Application, output is written to the command window using the statement <code>Console.WriteLine();</code>. Inside the parentheses we put a string value; that is, a section of text surrounded by quotation marks. Here are a few examples of strings:
- “Hello, World!”
- “Napoleon Bonaparte”
The last one looks like a number, but because it has quotes around it, it is treated as a string by the C# compiler. Try putting a second output command on the line after our Hello, World! statement. Use whatever string value you want!
To run the application, click the Start button at the top of the screen. A command window will pop up for a brief second and display “Hello, World!”, and on the next line, whatever string you put in Console.WriteLine();. To make this command window stay long enough to really see what we’ve done, add the following line after the Console.WriteLine you last added:
This will make the command window stay until you press the Enter key.
Reading from the Console
Now we will make the user input some information. To do this, we use:
string input = Console.ReadLine();
I should take this opportunity to introduce variables. Variables are objects that store information of the specified type. So,
string input = "input";
will save the value “input” to the string variable named input. The Console.ReadLine() statement takes no arguments (values inside the parentheses) and returns whatever the user types into the command window. Earlier, we used Console.ReadLine() to keep the command window visible after printing our lines of text. That was just a hack to make the command window stay. Console.ReadLine()’s intended use is to get user input.
Now remove the Console.ReadLine() we used earlier. In its place, type:
Console.WriteLine("What's your name?"); string name = Console.ReadLine(); Console.WriteLine("My name is " + name); Console.ReadLine();
Can you figure out what this does? First, we prompt the user for their name. Then, we read in their answer and store it in the variable named name. Finally, we print out “Hello, ” followed by their name. We can combine two string values by using a + operator. This is called concatenation. We will go over concatenation more in the tutorial over the string datatype.
The final line we just added just pauses the command window so we can see the result of our actions.
Here’s how the final program will look inside the Main method:
Console.WriteLine("Hello, World!"); Console.WriteLine("How are you today?"); Console.WriteLine("What is your name?"); string name = Console.ReadLine(); Console.WriteLine("My name is " + name); Console.ReadLine();
Of course, that second line will be whatever you wanted to print.
Now that you have learned how to create a console application, get user input, and write text to the command window, try to challenge yourself with what you know. Try to create a questionnaire that asks the user a series of questions and then prints their responses. If you have any questions, leave a comment or check out the Microsoft Developer Network to learn more about the C# compiler, Console Applications, or syntax mistakes you find.