In this tutorial you will learn how to begin programming in C#. You will learn how to create your first console application and create your first “Hello World” style application. We will be using Visual Studio 2010 for our IDE, but you can follow along in Visual Studio Express if you prefer.
Contributed by James Payne Rating: / 3 January 05, 2012
This article will assume that you have installed a copy ofVisual Studio or Visual Studio Express and that it is configured. You do notneed any programming skills, but knowing another language never hurts. We willbe creating a “Hello World” style application that asks for input (or data thatthe user types in) and then responds by printing some text to the user’smonitor. For those of you that are newto programming, a “Hello World” application is equivalent to learning “Smoke onthe Water” when you first pick up a guitar – it is a universal application usedto demonstrate programming basics.
To begin, let’s open up an instance of Visual Studio byclicking on Start - > All Programs -> Microsoft Visual Studio 2010, then click on the Microsoft VisualStudio 2010 program. This will open the Visual Studio Start Page:
From here you can launch or open an old Project, or – as inour case – create a new one. To do that, click on the New Project… link. The NewProject pane will appear, giving you a list of different types ofapplications you can begin (these are all templatized so that you do not haveto worry about setting up the basics of your application – MS Visual Studiotakes care of that for you). For simplicities sake, our first application isgoing to be a Console Application, which is a program that runs in your consolewindow, such as your MS-DOS Prompt or Command window:
Depending upon your setup, you may need to click Visual C# under the Installed Templatesbox, then choose Windows. Fromthere, you will see Console Application to the right, as highlighted above.
The next step is to name the program – for our purposeslet’s call it Test1. You can change the location the file is saved to byclicking on the Browse button located next to the Location text field, but Irecommend leaving the save location at default for now. Click the OK button tolaunch the newly created project.
You will see a page that looks like the followingscreenshot, under the Program.cs tab. The designation “.cs” refers to the filetype that C# programs are stored as.
Don’t be too afraid by everything you see here – this is alljust the basics you need to start a C# console application. We will get intothe gory details soon enough. For now all you need to know is that our “HelloWorld” application will be written between the
Staticvoid Main(string[] args)
{
//write code here
}
Curly brackets shown above. One interesting not before wecontinue: see the line
// write code here
That is known as a comment. When the interpreter (VisualStudio) sees the // on a line, it skips the text that follows it on that sameline. This is known as a comment, and is used to leave notes to yourself orother programmers who view your code in the future. It serves as a method ofdocumenting or describing what a piece of code does, in case it gets looked atin the future. Basically it makes it easier for someone to quickly know whatyou were trying to do with a piece of code.
To do multi-line commenting, just use another set of // oneach line, like so:
// write code here
// write more code here
Now that we know where the code is going to go and how towrite some comments, let’s create our program. Modify the code above so that itmatches this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test1
{
classProgram
{
//program beginning
staticvoid Main(string[] args)
{
//prompt for a username
Console.WriteLine("Enteryour stupid name: ");
//Capture the input
string stupidName = Console.ReadLine();
//print the text "Hello, " followed by the namethe user input
Console.WriteLine("Hello," + stupidName + ". What a stupidname.");
//prompt the user to press the Enter button and react tothat button
Console.WriteLine("PressEnter if your dumbass agrees...");
Console.Read();
}
}
}
To understand what each line of code does, read itsassociated comments. The important statements to understand are the following:
Console.WriteLine: This code will print a line of text to theConsole screen.
Example:Console.WriteLine("Enter your stupid name: ");
This code will print out the following text:
Enter Your Stupid Name
Console.ReadLine(): This code willcapture the text the user types in. Optionally, you can store this in avariable (more on this in our next article):
Example:
string stupidName = Console.ReadLine();
If the user types in the name “Manuel”, this data will bestored in the variable (think of it as a box that holds data) stupidName.
+: The plus sign is used to append or concatenate texttogether. The way we use it in our program is like so:
Console.WriteLine("Hello," + stupidName + ". What a stupidname.");
This starts off by writing the text:
Hello,
to the screen. Next, it looks for the value stored in thevariable stupidName (in this case, whatever the user typed in). Next, it seesanother “+” and appends the text:
. What a stupid name.
to the rest of the sentence, resulting in:
Hello, Manuel. What a stupid name.
To run our program, click on Debug, then Start WithoutDebugging.
Here are a few screenshots of the program in action: