Chapter 2: Basic Syntax and First Program
1. Our Very First Program: Hello World! (Modern & Classic Versions)
In C# (especially since C# 9 and above), there are two main ways to write the classic “Hello World” program.
Modern Way (C# 9+ / .NET 6+ – Recommended in 2026 – Super Clean!)
Open your project (the one we created last time), and replace everything inside Program.cs with this:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// This is a comment – the computer completely ignores it Console.WriteLine("Hello, World! 🌍"); Console.WriteLine("Welcome to C# programming in 2026! 🚀"); Console.WriteLine("I am learning with Webliance from Hyderabad! 😊"); // Optional: Wait before closing the window Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); |
Run it (F5 in Visual Studio or dotnet run in VS Code) and you’ll see:
|
0 1 2 3 4 5 6 7 8 9 10 |
Hello, World! 🌍 Welcome to C# programming in 2026! 🚀 I am learning with Webliance from Hyderabad! 😊 Press any key to exit... |
Why is this modern style so great? No class, no static void Main(), no curly braces everywhere – just clean, straight-to-the-point code!
Classic Way (Still very common – you’ll see this in older tutorials/books)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World! 🌍"); Console.WriteLine("This is the classic style!"); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); } } } |
Both versions do exactly the same thing – but the modern one is shorter and cleaner.
Recommendation for 2026: Use the modern top-level statements style (first version) for all your new projects – it’s what Microsoft recommends now!
2. Understanding the Program Structure (Even in Modern Style)
Even though the modern style hides a lot, let’s understand what’s really happening behind the scenes.
Modern Style (What you write)
|
0 1 2 3 4 5 6 |
Console.WriteLine("Hello!"); |
What the compiler actually sees (behind the scenes)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Hidden automatically by the compiler using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello!"); } } |
So when you write the short version, C# is automatically creating:
- A hidden using System;
- A hidden class Program
- A hidden static void Main() method
This is called Top-level statements – introduced in C# 9 and improved in later versions.
3. The Building Blocks Explained in Detail
Let’s break down the classic version so you really understand every part:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; // ← Import the System namespace (gives us Console) namespace HelloWorld // ← Logical grouping / project name { class Program // ← The class that contains our code { static void Main(string[] args) // ← The entry point – where program starts { // Your code goes here! Console.WriteLine("Hello, World!"); } } } |
| Part | What it means |
|---|---|
| using System; | Tells C#: “I want to use stuff from the System library” (like Console) |
| namespace HelloWorld | A container / folder for your code (helps avoid name conflicts) |
| class Program | Every C# program needs at least one class. This is our main class |
| static void Main() | The starting point of every console program. C# looks for this method first |
| string[] args | Optional: command-line arguments (we’ll use later) |
| Console.WriteLine() | Prints text to the screen and moves to the next line |
4. Comments – Super Important!
Comments are notes you write for yourself (or other developers). The computer completely ignores them.
There are three types of comments in C#:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Single-line comment – everything after // is ignored /* Multi-line comment Can span many lines Very useful for explaining big blocks of code */ /** * XML Documentation comment (special style) * Used to create automatic documentation * <summary>This method says hello</summary> */ |
Best practice: Always add comments explaining why you wrote something (not just what).
Example:
|
0 1 2 3 4 5 6 7 8 9 |
// Ask the user for their name and greet them personally Console.Write("What is your name? "); string name = Console.ReadLine(); Console.WriteLine($"Hello, {name}! Welcome to C#! 🌟"); |
5. Code Formatting & Style – Write Beautiful Code!
C# is very forgiving about spacing, but good formatting makes code easy to read.
Good style example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Good formatting – easy to read Console.WriteLine("Hello"); Console.WriteLine("World"); int age = 25; if (age >= 18) { Console.WriteLine("You are an adult!"); } |
Bad formatting (still works, but hard to read):
|
0 1 2 3 4 5 6 |
Console.WriteLine("Hello");Console.WriteLine("World");int age=25;if(age>=18){Console.WriteLine("You are an adult!");} |
Rules most developers follow (2026 style):
- 4 spaces (or 1 tab) for indentation
- Opening { on the same line as the statement
- One statement per line
- Space after commas, around operators
- Meaningful blank lines between logical sections
6. Mini-Experiment: Play with Hello World!
Try modifying the program yourself:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
Console.WriteLine("╔════════════════════════════╗"); Console.WriteLine("║ Hello from Hyderabad! ║"); Console.WriteLine("║ Webliance is learning ║"); Console.WriteLine("║ C# in 2026! ║"); Console.WriteLine("╚════════════════════════════╝"); Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); |
Run it – see your beautiful box!
Summary – What We Learned Today
- How to write Hello World in modern C# (top-level statements)
- The classic structure: using, namespace, class, Main
- What happens behind the scenes with top-level statements
- Three types of comments
- Why good formatting matters
Your Homework (Fun & Easy!)
- Create a new console project called MyGreeting
- Write a program that:
- Asks for the user’s name
- Asks for their favorite color
- Prints a colorful welcome message like:
text01234567Hello, [Name]!Your favorite color is [Color] – that's awesome! 🌈
- Add nice comments explaining each part
- Format the code beautifully
Next lesson: Variables, Data Types, and Basic Operations – we’re going to start making programs that actually do calculations!
You’re doing fantastic! 🎉 Any questions? Want me to explain any part again? Just ask – I’m right here for you! 💙
