Writting Your First C# Program
Posted by codeincsharp on October 17, 2009
I am assuming you already have Visual Studio or Visual Studio Express installed and are ready to start programming with C#. If not, you can download and install Visual C# 2008 Express Edition here. Once you have Visual Studio installed, perform the following steps to create a new project:
- Start Visual Studio.
- Go to File menu > New, and then click Project.
- In the new project screen, under Visual C#, select Windows.
- In the Templates pane, select Console Application.
- Type a name for your project. (I used “HelloWorld”)
- Click OK.
At this point, you should be looking at the program.cs source code, and it should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
}
}
}
What we want to do is add code to the Main method, that writes a message to the console that says “Hello World”. Modify the main method so it looks like this:
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
Press F5 to run the project. You should get a black command prompt that displays the text “Hello World”.
Console.WriteLine is the method that writes the text to the console. We use the Console.ReadKey method to have our application pause, waiting for input. If we did not do this the console application would close immediately after running the project.
Taking it a step further
Now that we’ve got a working C# application, let’s make it a little more interactive. Modify your Main method to use the following code:
static void Main(string[] args)
{
string name;
Console.WriteLine("Please enter your name:");
name = Console.ReadLine();
Console.WriteLine("Hello {0}, Welcome to C#!", name);
Console.ReadKey();
}
When you run the project, you’ll get prompted to enter your name. When you enter your name, a message will be displayed on the screen that says hello to you. For example, if I enter John Doe as my name, the application displays “Hello John Doe, Welcome to C#!”.
Let me explain what is going on in this example.
- First, we declare a string variable called “name” (I’ll cover variables further in a later post).
- Next, we again use Console.WriteLine to display a message, asking the user for their name.
- We then assign the Console.ReadLine method to the name variable. This allows us to prompt the user for their name, and store the string in the name variable.
- Finally, we write a message to the console saying hello to the user name that was entered into our application. You may notice the syntax in this line is a little different. This is because we are using string formatting, I’ll cover strings in detail in a future post.
Congratulations, you’ve just written your first working C# program. In the coming posts I’ll be covering all of the basics to get you up and running and programming in C#.
Working with the Console Class « Code in C# said
[...] Writting Your First C# Program [...]