First C# Program, Top-Level Statements & Console I/O Masterclass

โšก C# 12 & .NET 8 ๐ŸŸข Chapter 3 of 35 ๐Ÿ“‚ Phase 1: First C# Program ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: Program.cs Anatomy ยท using Directives ยท Namespace ยท Class ยท Main() Method ยท Top-Level Statements ยท Console.WriteLine() ยท Console.Write() ยท String Interpolation ยท Comments

Welcome to Phase 1 (Chapter 3): First C# Program, Program.cs, Top-Level Statements & Console I/O Masterclass! Writing your first program is an essential rite of passage. In this lesson, we dissect C# program anatomy line-by-line, compare classic C# boilerplate with C# 9+ Top-Level Statements, master console output using Console.WriteLine() and Console.Write(), and explore string interpolation and comments.

1Classic Program Structure vs Top-Level Statements

In traditional C# (versions 1.0 through 8.0), every program required explicit using directives, a namespace declaration, a class, and a static Main() entry method. Modern C# 9+ introduced Top-Level Statements, allowing you to write executable statements directly in Program.cs.

C# โ€” Classic Structure (C# 1.0 โ€“ 8.0) โ–ถ Run in Compiler
using System;

namespace HelloCSharpApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World from Classic C#!");
        }
    }
}
C# โ€” Modern Top-Level Statements (C# 9.0+) โ–ถ Run in Compiler
// Top-Level Statement โ€” Roslyn compiler auto-generates Program class and Main() method!
Console.WriteLine("Hello, C# 12!");
2Line-by-Line Anatomy Breakdown
C# โ€” Program.cs Detailed Breakdown โ–ถ Run in Compiler
using System; // 1. using directive: imports System namespace containing Console class

namespace MyFirstApp // 2. namespace: logical container organizing classes and preventing collisions
{
    class Program // 3. class: reference type blueprint containing fields and methods
    {
        // 4. Main method: mandatory entry point invoked by CLR runtime on startup
        static void Main(string[] args)
        {
            // 5. Console.WriteLine(): writes specified string to console and appends newline
            Console.WriteLine("Hello, C#!");

            // 6. Semicolon (;): mandatory statement terminator in C#
        }
    }
}
๐Ÿ” Detailed Element Breakdown:
  • using System;: Tells the compiler to look in the System namespace for types like Console and Math without requiring fully qualified names (like System.Console.WriteLine).
  • namespace: Declares a logical scope to organize code cleanly and avoid name clashes across libraries.
  • class Program: C# is strictly object-oriented โ€” all code must exist inside a class or struct.
  • static void Main(string[] args): The entry point method called by the CLR. static means it runs without instantiating the class; void means it returns no value; string[] args receives command-line parameters.
  • Console.WriteLine(): Invokes the WriteLine method of the built-in Console class.
  • ; (Semicolon): Every statement in C# must end with a semicolon.
3Console.WriteLine() vs Console.Write() & String Interpolation

C# provides two primary console output methods:

C# โ€” Write vs WriteLine & Interpolation โ–ถ Run in Compiler
// Console.Write() outputs text WITHOUT appending a newline character
Console.Write("Welcome ");
Console.Write("to ");
Console.Write("C#!
"); // Output: Welcome to C#!

// Console.WriteLine() outputs text AND automatically appends a newline (
)
Console.WriteLine("Line 1");
Console.WriteLine("Line 2");

// String Interpolation ($) โ€” Modern, clean way to format variables inside strings
string studentName = "Ravi";
int score = 95;
Console.WriteLine($"Student Name: {studentName}, Score: {score}");
4Comments in C#
C# โ€” Comment Styles โ–ถ Run in Compiler
// 1. Single-line comment โ€” ignored by compiler

/* 2. Multi-line comment
      spans across multiple lines */

/// <summary>
/// 3. XML Documentation Comment โ€” generates IntelliSense tooltips
/// Calculates total price after tax.
/// </summary>
static double CalculateTax(double price)
{
    return price * 1.18;
}
5Technical FAQs

Q1: Can I have multiple Top-Level Statements in a project?

No. Only ONE file in a project can use Top-Level Statements (usually Program.cs). Other files must declare classes explicitly.

Q2: Is C# case-sensitive?

Yes! C# is strictly case-sensitive. Console.WriteLine, console.writeline, and CONSOLE.WRITELINE are completely different identifiers, and only the exact capitalized version compiles.