First C# Program, Top-Level Statements & Console I/O Masterclass
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.
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.
using System;
namespace HelloCSharpApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World from Classic C#!");
}
}
}
// Top-Level Statement โ Roslyn compiler auto-generates Program class and Main() method!
Console.WriteLine("Hello, C# 12!");
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#
}
}
}
using System;: Tells the compiler to look in theSystemnamespace for types likeConsoleandMathwithout requiring fully qualified names (likeSystem.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.staticmeans it runs without instantiating the class;voidmeans it returns no value;string[] argsreceives command-line parameters.Console.WriteLine(): Invokes theWriteLinemethod of the built-inConsoleclass.;(Semicolon): Every statement in C# must end with a semicolon.
C# provides two primary console output methods:
// 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}");
// 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;
}
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.