Beginning C# programming with Hello World

The following is a simple "Hello World" program in C# programming. This is typically the first program you write when learning a new language, and it demonstrates the basic structure of a C# application.

Syntax

Following is the basic structure of a C# program −

using System;

namespace NamespaceName {
   class ClassName {
      static void Main(string[] args) {
         // Your code here
      }
   }
}

Hello World Example

using System;

namespace MyHelloWorldApplication {
   class MyDemoClass {
      static void Main(string[] args) {
         // display text
         Console.WriteLine("Hello World");
         // display another text
         Console.WriteLine("Welcome!");
      }
   }
}

The output of the above code is −

Hello World
Welcome!

Understanding the Code

Let us see now what each part of the program does −

Structure of a C# Program using System; ? Import namespace namespace MyHelloWorldApplication ? Container class MyDemoClass ? Class definition static void Main(string[] args) ? Entry point of the program Console.WriteLine("Hello World"); Console.WriteLine("Welcome!"); ? Prints text to the console

  • using System − The using keyword includes the System namespace in the program. This namespace contains fundamental classes like Console.

  • namespace declaration − A namespace is a collection of classes. The MyHelloWorldApplication namespace contains the class MyDemoClass.

  • class − The class MyDemoClass contains the data and method definitions that your program uses. Classes generally contain multiple methods.

  • Main method − The Main method is the entry point for all C# programs. It defines what the class does when the program is executed.

  • Console.WriteLine()WriteLine is a method of the Console class defined in the System namespace. This statement prints the specified text to the console, followed by a new line.

NoteConsole.ReadKey() makes the program wait for a key press, preventing the console window from closing immediately when running from Visual Studio. It is not needed when running in an online compiler.

Using String Interpolation

C# also supports string interpolation using the $ prefix, which allows you to embed variables directly inside a string −

Example

using System;

class Program {
   static void Main(string[] args) {
      string name = "TutorialsPoint";
      int year = 2025;
      Console.WriteLine($"Welcome to {name} in {year}!");
   }
}

The output of the above code is −

Welcome to TutorialsPoint in 2025!

Using Multiple Console Methods

The Console class provides both WriteLine() (prints with newline) and Write() (prints without newline) −

Example

using System;

class Program {
   static void Main(string[] args) {
      Console.Write("Hello ");
      Console.Write("World ");
      Console.WriteLine("- using Write()");
      Console.WriteLine("This is on a new line - using WriteLine()");
   }
}

The output of the above code is −

Hello World - using Write()
This is on a new line - using WriteLine()

Write() keeps the cursor on the same line, while WriteLine() moves it to the next line after printing.

Conclusion

Every C# program begins with a Main method inside a class. The using System directive provides access to essential classes like Console. Understanding this basic structure ? namespace, class, Main method, and console output ? forms the foundation for writing all C# applications.

Updated on: 2026-03-17T00:01:23+05:30

787 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements