-
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:
Now, let’s understand each part of this program in detail.Hello World!
1. using System;
The first line of the program is:using System;The
usingkeyword is used to include namespaces in a C# program.
A namespace is a collection of related classes, interfaces, and methods.The
Systemnamespace 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
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
Programclass 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 isMainimportant?- Execution of a C# program always starts from
Main - The program stops when Main finishes executing
static→ allows the method to run without creating an object of the classvoid→ means the method does not return any valuestring[] args→ allows command-line arguments to be passed to the program
5. Console.WriteLine("Hello World!");
This statement prints text to the console.Console.WriteLine("Hello World!");Consoleis a class inside the System namespaceWriteLineis a method that displays text and moves the cursor to a new line"Hello World!"is a string literal enclosed in double quotes
6. Console.ReadKey();
This line waits for the user to press a key before closing the console window.Console.ReadKey();
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.
These are treated as different identifiers.MyClass ≠ myclass
2. Every Statement Ends with a Semicolon (;)
In C#, each instruction must end with a semicolon:
Missing semicolons will cause compilation errors.Console.WriteLine("Hello World!");
3. Code Blocks Use Curly Braces { }
Curly braces define the beginning and end of:- namespaces
- classes
- methods
A Slightly Extended Example
Output: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(); } } }
This example shows how multiple statements can be written inside theWelcome to C# Learning syntax step by stepMainmethod.
Summary
A C# program starts execution from the
Mainmethod and is organized using namespaces and classes. Theusingstatement allows access to built-in libraries, whileConsole.WriteLinedisplays 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.