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:

C#

Run it (F5 in Visual Studio or dotnet run in VS Code) and you’ll see:

text

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)

C#

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)

C#

What the compiler actually sees (behind the scenes)

C#

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:

C#
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#:

C#

Best practice: Always add comments explaining why you wrote something (not just what).

Example:

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:

C#

Bad formatting (still works, but hard to read):

C#

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:

C#

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!)

  1. Create a new console project called MyGreeting
  2. Write a program that:
    • Asks for the user’s name
    • Asks for their favorite color
    • Prints a colorful welcome message like:
      text
  3. Add nice comments explaining each part
  4. 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! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *