Skip to content

C# Hello World

A simple application in C#.

Hello World is a traditional first application for a programmer learning a new language. It is a simple program that prints out a message to a display device and is a good way to learn the basic syntax of a language.

A simple Hello World example:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

C# Hello World Walkthrough

Step One

Launch Visual Studio and start a new project. There are several ways to start a new project. You can select if from the start page, from the File >New > Project... menu option, or by using the keyboard shortcut ctrl-shift-N.

C# Hello World: New project

C# Hello World

Step Two

Make a new Visual C# Console application and name it HelloWorld.

C# Hello World

Step Three

Copy the following code:

    
Console.WriteLine("Hello World!");
Console.ReadLine();

Step Four

Visual Studio has generated a lot of code for you. Paste the code from above inside the main function. Your finished code should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

C# Hello World

Step Five

Press the Start button at the top

start

You should now see something like this:

C# Hello World

Congratulations! Now try to add another line to be printed out to the screen with another message.

Code Notes

Programming Tips

With this simple example you can see fundamental aspects of programming in C#. Here are a few notes:

  • Spelling and capitalization are important. The compiler will think that Main() is different from main(). Make sure you capitalize and spell keywords and identifiers correctly.
  • Main() is a method (also sometimes called a function, behavior, or operation). It is the 'entry' point into the application; the first thing that runs when your application is launched. Every C# application has one single entry point, and by default it is Main(). The C# Console Application template writes Main() as: Main(string[] args). That is one way in which it can be used (to take in an array of arguments from the command line when you start the application). You can also use Main() without anything in the parentheses like this: Main().
  • Parentheses: After the method name, there are parentheses that can hold arguments (parameters) that could change how the method will run.
  • A statement tells your application something to do - any time your program does something, it is because a statement was executed.
  • Punctuators help demarcate the structure of a program. For example, curly braces are used for a code block, and a semicolon terminates a statement. If you leave out a semicolon or a brace, your code might not compile and run. Pay close attention to punctuators and make sure you are using them correctly.
  • Code blocks are surrounded by curly braces {}. These are statements grouped together for a purpose. For example, statements within a function's curly braces will run when that function is invoked or called.
  • Semicolons terminate a statement. Every statement needs to have a semicolon to 'end' it unless it is a code block.
  • Quotation marks: surround a string with double quote marks. If you'd like to include double quotation marks inside a string, use escape characters so that the compiler doesn't think you are ending the string (like this: \").
  • Your file names should have no spaces. You can use multiple words, just connect them. For example, My File could be written as: MyFile, My_File, My-File, or myFile.
  • File extension: .cs. Visual Studio creates many files for your project. Your code is stored in files that have the extension '.cs'.
  • You can make a C# application without using an IDE (Integrated Development Environment). The language and the IDE are different - one doesn't need the other. Using an IDE like Visual Studio, though, can make debugging much easier.

More advanced concepts:

  • public: This keyword allows access from code outside of your class. Making sections of your code private is a good practice; it controls access. However when you are first learning C# it is often easier to make everything public until you learn about access modifiers.
  • static: A class is like a blueprint - you can create many objects from the class. Each object is instantiated (an "instance" of the class is created - similar to creating a house from a blueprint). Once instantiated with a unique identifier, you can access the properties and methods of the object. In the above example we have not instantiated our object. Instead we are calling on a method directly from the blueprint (the class) instead of calling on a method of a specific object. In order to do this, we need to use the keyword static.


The ACM Hello World site has examples of Hello World in multiple languages.

(This walkthrough shows screenshots made with Visual Studio 2012 on Windows 7.)

Object-Oriented Programming

Even with a simple example such as Hello World, you can see how C# is an object-oriented programming language. The class in the example above, HelloWorld, has one method: Main(). More complex applications may have classes with more than one method, and that have fields (properties).

A simple UML diagram of HelloWorld:

HelloWorld
 
Main()

  • The top section is the class name
  • The middle section (fields/properties) is blank since our code above has no information about the class needed
  • The bottom section (methods/behaviors/operations) shows what our object can "do". In this case there is only one action - the Main function of our program that contains a statement to print "Hello World" to the console window.