Code in C#

Learn How to Program in C#

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:

  1. Start Visual Studio.
  2. Go to File menu > New, and then click Project.
  3. In the new project screen, under Visual C#, select Windows.
  4. In the Templates pane, select Console Application.
  5. Type a name for your project. (I used “HelloWorld”)
  6. 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#.

Advertisement

One Response to “Writting Your First C# Program”

  1. [...] Writting Your First C# Program [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.