C# Program and Syntax

When learning any programming language, the very first step is understanding how a basic program works. In C#, this usually starts with writing a simple Hello World program. Although the program looks small, it introduces many important concepts such as namespaces, classes, methods, and statements.


A Simple C# Hello World Program

Below is a basic C# program that prints “Hello World” to the screen:

using System;

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

When this program is compiled and executed, it produces the following output:

Output:

Hello World!
Now, let’s understand each part of this program in detail.

1. using System;

The first line of the program is:

using System;

The using keyword is used to include namespaces in a C# program.
A namespace is a collection of related classes, interfaces, and methods.

The System namespace contains many built-in classes such as:

  • Console
  • String
  • Math
  • DateTime

By writing using System;, we tell the compiler that our program will use classes defined inside the System namespace. Without this line, we would need to write fully qualified names like
System.Console.WriteLine().


2. namespace HelloWorld

A namespace is used to organize code logically and avoid naming conflicts in large applications.


namespace HelloWorld
{
}

Think of a namespace as a container that groups related classes together.
In real-world projects, namespaces help structure applications into meaningful sections such as:

  • MyApp.Services
  • MyApp.Models
  • MyApp.Controllers
Here, HelloWorld is simply the name of our namespace.

3. class Program


class Program
{
}

A class is a blueprint that defines the structure and behavior of objects.
In C#, all code must be written inside a class.

The Program class contains:

  • variables (data)
  • methods (behavior)

4. static void Main(string[] args)


static void Main(string[] args)
{
}

This line defines the Main method, which is the entry point of every C# application.

Why is Main important?
  • Execution of a C# program always starts from Main
  • The program stops when Main finishes executing
Meaning of each keyword:
  • static → allows the method to run without creating an object of the class
  • void → means the method does not return any value
  • string[] args → allows command-line arguments to be passed to the program

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


Console.WriteLine("Hello World!");
This statement prints text to the console.
  • Console is a class inside the System namespace
  • WriteLine is a method that displays text and moves the cursor to a new line
  • "Hello World!" is a string literal enclosed in double quotes
This is why the message appears on the screen when the program runs.

6. Console.ReadKey();


Console.ReadKey();

This line waits for the user to press a key before closing the console window.
This line is optional but helpful for beginners.

Important Rules to Remember in C#

1. C# Is Case-Sensitive

C# distinguishes between uppercase and lowercase letters.

MyClassmyclass

These are treated as different identifiers.

2. Every Statement Ends with a Semicolon (;)

In C#, each instruction must end with a semicolon:

Console.WriteLine("Hello World!");
Missing semicolons will cause compilation errors.

3. Code Blocks Use Curly Braces { }

Curly braces define the beginning and end of:
  • namespaces
  • classes
  • methods
They help structure the code clearly.

A Slightly Extended Example


using System;

namespace SampleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to C#");
            Console.WriteLine("Learning syntax step by step");
            Console.ReadKey();
        }
    }
}
Output:

Welcome to C#
Learning syntax step by step
This example shows how multiple statements can be written inside the Main method.

Summary

A C# program starts execution from the Main method and is organized using namespaces and classes. The using statement allows access to built-in libraries, while Console.WriteLine displays output to the user. Understanding this basic program helps beginners build a strong foundation for learning advanced C# concepts such as object-oriented programming, file handling, and application development.