Your First C# Program

Vaibhav • September 11, 2025

Now that your development environment is ready, it’s time to write your very first C# program. In this article, we’ll walk through the structure of a basic C# application, explain how it runs, and highlight common mistakes beginners often make. By the end, you’ll not only understand what each line of code does-you’ll also feel confident writing and running simple programs on your own.

Understanding the Main Method

Every C# application starts with a special method called Main. This is the entry point of your program-the place where execution begins. Let’s look at a minimal example:

using System;

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

Let’s break this down line by line:

  • using System; - This tells the compiler that we want to use the System namespace, which contains useful classes like Console.
  • class Program - This defines a class named Program. In C#, all code must live inside a class.
  • static void Main(string[] args) - This is the Main method. It’s where the program starts running. The string[] args part allows the program to accept command-line arguments (we’ll explore that later).
  • Console.WriteLine("Hello, World!"); - This prints a message to the screen.

The Main method must be static because it’s called by the runtime before any objects are created.

Console.WriteLine Explained

The Console.WriteLine method is your first tool for interacting with the user. It sends output to the terminal or console window.

Console.WriteLine("Welcome to C#!");

This line prints the text inside the quotation marks and then moves the cursor to the next line. If you want to print without moving to a new line, you can use Console.Write instead:

Console.Write("This stays ");
Console.Write("on the same line.");

Output:

This stays on the same line.

You can use Console.WriteLine to print numbers, variables, and even combine text:

Console.WriteLine("The answer is " + 42);

This will output:

The answer is 42

You can use + to concatenate (join) strings and values. C# automatically converts numbers to strings when needed.

How Compilation and Execution Work

When you write C# code, it doesn’t run directly. It goes through a process:

  1. Compilation: Your code is converted into an intermediate language (IL) by the C# compiler.
  2. Execution: The .NET runtime (CLR) reads the IL and executes it on your machine.

This two-step process allows C# programs to run on different platforms using the same codebase.

You don’t need to worry about IL or CLR right now. Just know that dotnet run handles both compilation and execution for you.

Here’s what happens when you run:

dotnet run

Behind the scenes, this command:

  • Compiles your code into a .dll file
  • Passes it to the .NET runtime
  • Executes the Main method

Common Beginner Mistakes

Everyone makes mistakes when starting out. Here are a few common ones-and how to avoid them.

Missing Semicolons

In C#, every statement must end with a semicolon (;). Forgetting it causes a compilation error.

// ❌ Missing semicolon
Console.WriteLine("Oops") 

// ✅ Correct
Console.WriteLine("All good");

Unmatched Braces

C# uses curly braces ({ }) to define blocks of code. If you forget to close a brace, the compiler will complain.

// ❌ Missing closing brace
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello!");
    // ← missing this }

Always make sure every opening brace has a matching closing brace. Most editors will help you with this.

Case Sensitivity

C# is case-sensitive. Main is not the same as main, and Console is not the same as console.

// ❌ Incorrect
console.writeline("Hi");

// ✅ Correct
Console.WriteLine("Hi");

Wrong File Location

If you try to run dotnet run from the wrong folder (not where your .csproj file is), you’ll get an error.

Always navigate to your project folder before running commands. Use cd to change directories.

Experimenting with Output

Try modifying your program to print different messages. For example:

Console.WriteLine("Welcome to C# programming!");
Console.WriteLine("This is your first step.");
Console.WriteLine("Let's build something great.");

You can also print blank lines:

Console.WriteLine();

This simply moves the cursor to the next line without printing anything.

What Happens If You Make a Mistake?

Let’s say you forget a semicolon:

Console.WriteLine("Hello")

When you run dotnet run, you’ll see an error like:

error CS1002: ; expected

This means the compiler expected a semicolon but didn’t find one. Don’t worry-errors are part of the learning process. Read the message, fix the issue, and try again.

The compiler is your friend. It catches mistakes before your program runs, helping you write safer code.

Summary

In this article, you wrote and explored your first C# program. You learned:

  • How the Main method serves as the entry point
  • How to use Console.WriteLine to display output
  • What happens during compilation and execution
  • How to avoid common beginner mistakes like missing semicolons and braces

These fundamentals will serve as the foundation for everything you build in C#. In the next article, we’ll explore what .NET and the CLR are, and how they power your C# applications behind the scenes.