Your First C# Program

The shortest complete C# program is one line:

Concept

The shortest complete C# program is one line:

Console is a class from the System namespace; WriteLine is a static method that prints text and a newline. System is imported automatically by *implicit usings* in modern projects.

Since C# 9, statements can be written directly in one file without declaring a class or Main method. The compiler generates a hidden Program class with an entry point for you:

Only one file per project may contain top-level statements.

The command-line arguments are available as args (a string[]).

return with an int sets the process exit code; await may be used directly (the generated Main becomes async).

Types (classes, records) may be declared *after* the top-level statements in the same file.

The Traditional Main Method Before C# 9 (and still common in larger apps), the entry point is an explicit static method named Main:

Valid signatures: static void Main(), static int Main(), static void Main(string[] args), static int Main(string[] args), and async Task / async Task<int> versions.

Multi-line (block) comment /// <summary>…</summary> XML documentation comment — shown in IntelliSense and exportable to docs

Console.WriteLine("Hello, World!");
class Program
{
    static void Main(string[] args) { /* ... */ }
}

Why It Matters

The entry point is where every program starts; arguments and exit codes are how command-line tools, scripts and CI pipelines talk to your program.

Syntax

// Top-level program
Console.WriteLine("text");
return 0;                 // optional exit code

// Classic program
static int Main(string[] args) { return 0; }

Example

3.1 Top-level statements with arguments and an exit code Run with arguments: dotnet run Program.cs -- ECU-7 --verbose 42.

3.2 Classic Main with comments

// A top-level program: the compiler generates the Main method for you.
// 'args' is available automatically.
Console.WriteLine($"Arguments received: {args.Length}");
for (int i = 0; i < args.Length; i++)
{
    Console.WriteLine($"  args[{i}] = {args[i]}");
}

int exitCode = args.Length == 0 ? 1 : 0;
Console.WriteLine($"Returning exit code {exitCode}");
return exitCode;   // top-level statements may return an int exit code
namespace SampleApp.Intro;

/// <summary>A traditional program with an explicit entry point.</summary>
class Program
{
    static void Main(string[] args)
    {
        // Single-line comment: explains *why*, not *what*
        Console.WriteLine("Hello from Main");

        /* Multi-line comment:
           useful for temporarily disabling a block */
        Greet("C#");
    }

    /// <summary>XML documentation comments appear in IntelliSense.</summary>
    static void Greet(string language) => Console.WriteLine($"Welcome to {language}!");
}

Expected Result

Top-level program (arguments ECU-7 --verbose 42):

Classic Main program:

Check the exit code in a shell: echo $? (bash) or echo %ERRORLEVEL% (cmd) prints 0; run without arguments and it prints 1.

Arguments received: 3
  args[0] = ECU-7
  args[1] = --verbose
  args[2] = 42
Returning exit code 0
Hello from Main
Welcome to C#!

Common Mistakes

Putting top-level statements in two files (error CS8802: only one compilation unit can have top-level statements).

Declaring types *before* top-level statements in the same file (error CS8803).

Writing main instead of Main — C# is case-sensitive, so no entry point is found (CS5001).

Forgetting that args does not contain the program name (unlike C's argv[0]).

Best Practices

Use top-level statements for small tools and samples; use an explicit Program class (or a host builder) for larger apps.

Return non-zero exit codes on failure so scripts can react.

Write /// documentation comments for public APIs, and ordinary comments to explain *why*.

Interview Relevance

Low–medium: "What are top-level statements?", "What are the valid Main signatures?", "How do you return an exit code?".

Practice Questions

Print your name and the current date (DateTime.Now).

Print the number of arguments and all arguments in reverse order.

Convert the top-level program to a classic Main method.

Return exit code 2 when the first argument is --fail.

Quick Quiz

Q1. (True/False) Two files in the same project may both contain top-level statements.

Answer: False. Only one file may; the compiler generates a single entry point from it.

Q2. (Predict the output) With arguments a b, what does Console.WriteLine(args.Length); print in a top-level program?

Answer: 2 — args holds only the arguments, not the program name.

Q3. (Multiple choice) Which comment style feeds IntelliSense documentation? A) // B) /* */ C) /// D) # Answer: C.

Q4. (Find the bug) static void main(string[] args) { } — why is there no entry point?

Answer: The method must be named Main with a capital M; C# identifiers are case-sensitive.

Coding Challenge

Write a top-level program that greets the name passed as the first argument, or prints Usage: greet <name> and returns exit code 1 when no argument is given.

if (args.Length == 0)
{
    Console.WriteLine("Usage: greet <name>");
    return 1;
}
Console.WriteLine($"Hello, {args[0]}!");
return 0;