C# Introduction

Find out what C# is and what it's used for, set up the .NET SDK, run your first program with Console.WriteLine, and learn how statements, comments and top-level programs work.

Lesson 1 of 43Beginner18 min read

C# (pronounced "C sharp") is a modern, general-purpose programming language created by Microsoft. It's used to build web applications and APIs, cloud services, Windows and cross-platform desktop apps, mobile apps, and a huge number of games: the Unity game engine uses C# for its scripts, so many indie and mobile games are written in it.

C# runs on .NET, an open-source platform that works on Windows, macOS and Linux. .NET compiles your code, manages memory for you, and provides an enormous standard library for files, networking, JSON, dates and much more. C# is statically typed: every variable has a type the compiler checks, which catches many mistakes before the program runs.

C# is a great language to learn. Its syntax is familiar to anyone who later meets Java, C++ or JavaScript, its tooling is excellent, and it's in steady demand in businesses of every size. In this lesson you'll set up .NET, run your first program and learn the basic rules every C# file follows.

Setting up and running C#

  1. Install the .NET SDK from Microsoft's dotnet website. It includes the compiler and the dotnet command.
  2. Create a project and run it from a terminal:
Terminal
dotnet new console -o HelloApp
cd HelloApp
dotnet run

dotnet new console creates a folder with a project file (HelloApp.csproj) and a Program.cs file. dotnet run compiles and runs it. Popular editors are Visual Studio on Windows, Visual Studio Code with the C# Dev Kit extension, and JetBrains Rider.

Your first program

Modern C# lets Program.cs contain your statements directly, which are called top-level statements:

C#
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C#");
  • Console.WriteLine prints text followed by a new line; Console.Write prints without a new line.
  • Text in double quotes is a string.
  • Every statement ends with a semicolon.
  • Older projects wrap the same code in class Program { static void Main() { ... } }. That's what top-level statements generate behind the scenes, and you'll still see it in many tutorials and codebases.

Comments and case sensitivity

C#
// A single-line comment
/* A comment
   over several lines */
/// <summary>XML documentation comments describe methods for editors.</summary>

C# is case-sensitive: Console and console are different names, and only the first one exists. By convention, C# uses PascalCase for classes and methods (WriteLine, OrderService) and camelCase for local variables (totalPrice).

What happens when you type dotnet run

dotnet run hides several steps. First it restores any packages the project depends on, then it builds the project, and finally it starts the program it just built.

The C# compiler doesn't turn your code straight into machine code. It produces IL (Intermediate Language), stored in a .dll file inside bin/Debug/net8.0/ (the folder name follows the version of .NET you target). When the program starts, the .NET runtime's JIT (just-in-time) compiler translates that IL into machine code for your processor. This is why the same .dll can run on Windows, macOS and Linux, as long as .NET is installed.

You can run each step yourself:

Terminal
dotnet build        # compile only; errors appear without running anything
dotnet run          # build if needed, then run
dotnet watch        # rebuild and rerun every time you save a file

The obj folder holds temporary build files and bin holds the output. Both are recreated on every build, so you can delete them safely and you shouldn't commit them to Git.

The project file and implicit usings

Open HelloApp.csproj and you'll see a short XML file. The important lines look like this:

XML
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
  • OutputType Exe means this project is a program you can run, rather than a library for other projects.
  • TargetFramework picks the .NET version your code is built for.
  • ImplicitUsings adds common using directives for you, including System, System.IO, System.Linq and System.Collections.Generic. That's why Console.WriteLine works in a brand-new Program.cs that has no using System; line.
  • Nullable turns on warnings that help you avoid null-reference crashes, which you'll meet in later lessons.

You rarely need to edit this file by hand at first. Commands such as dotnet add package update it for you.

C# Basics Syntax

Program structure

// Program.cs with top-level statements
Console.WriteLine("Hello");
Console.Write("No new line");

// The classic structure
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello");
    }
}

C# Basics Summary

  • C# is a statically typed, general-purpose language that runs on .NET.
  • It's used for web APIs, cloud services, desktop apps and Unity games.
  • dotnet new console creates a project; dotnet run builds and runs it.
  • Console.WriteLine prints a line; statements end with semicolons.
  • C# is case-sensitive; classes and methods use PascalCase.

C# Basics Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Hello, World

C#
Console.WriteLine("Hello, World!");
Console.WriteLine("C# is fun.");
Output
Hello, World!
C# is fun.
Line-by-line explanation
  1. Line 1WriteLine prints the text and moves to a new line.
  2. Line 2Each statement ends with a semicolon.
Example 2

Write versus WriteLine

C#
Console.Write("Loading");
Console.Write(".");
Console.Write(".");
Console.WriteLine(".");
Console.WriteLine("Done!");
Console.WriteLine();
Console.WriteLine(7 + 3);
Output
Loading...
Done!

10
Line-by-line explanation
  1. Lines 1 to 3Write keeps printing on the same line.
  2. Line 4WriteLine ends the line after printing.
  3. Line 6WriteLine with nothing prints an empty line.
  4. Line 7Without quotes, 7 + 3 is a calculation, so it prints 10.
Example 3

The classic Main method

C#
using System;

namespace Greeter
{
    class Program
    {
        static void Main(string[] args)
        {
            // Program starts here
            Console.WriteLine("Starting the greeter");
            Console.WriteLine("Hello from Main");
        }
    }
}
Output
Starting the greeter
Hello from Main
Line-by-line explanation
  1. Line 1using lets you write Console instead of System.Console; new projects add it automatically.
  2. Line 3A namespace groups related code.
  3. Line 7Main is the entry point, where the program starts.
  4. Line 9The comment is ignored by the compiler.

Common C# Basics Mistakes

A missing semicolon

Don't
Console.WriteLine("Hello")     // error CS1002: ; expected
Do
Console.WriteLine("Hello");

Wrong capitalisation

Don't
console.writeline("Hello");    // error CS0103: The name 'console' does not exist
Do
Console.WriteLine("Hello");

Single quotes for text

Don't
Console.WriteLine('Hello');    // error: too many characters in character literal
Do
Console.WriteLine("Hello");    // single quotes are for one char, like 'A'

C# Basics in Real Projects

Where C# is used

C# powers websites and APIs built with ASP.NET Core, including large parts of Microsoft's own services and Stack Overflow. Game studios write gameplay code in C# with Unity, which runs on phones, consoles and PCs. Companies build internal business tools, Windows apps and cross-platform apps with .NET MAUI, and run C# services on Azure, AWS and Linux servers.

C# Basics FAQ

What is the difference between C# and .NET?

C# is the programming language: the keywords, syntax and rules you write. .NET is the platform that compiles and runs that code, and it provides the runtime and the huge standard library (Console, File, HttpClient and so on). Other languages, such as F# and Visual Basic, also run on .NET.

What is the difference between .NET Framework and .NET?

.NET Framework is the original Windows-only version, which now only receives maintenance updates (its last major version is 4.8). Modern .NET, once called .NET Core, is open source and cross-platform, and it's where all new features go. For new projects, install the current .NET SDK; even-numbered releases such as .NET 8 are long-term support versions.

Do I need Visual Studio to write C#?

No. The .NET SDK is enough: you can write code in any text editor and use dotnet new, dotnet build and dotnet run from a terminal. Visual Studio, Visual Studio Code with the C# Dev Kit, and JetBrains Rider add helpful extras such as autocompletion, debugging and refactoring.

Is C# similar to Java?

Yes, quite. Both are statically typed, object-oriented languages with C-style syntax, classes, garbage collection and a large standard library, so moving between them is easy. They differ in details: C# has properties, async/await, LINQ, structs and records built in, and it runs on .NET rather than the Java Virtual Machine.

Why does my Program.cs have no class or Main method?

Since C# 9, a project can use top-level statements, and new console projects do so by default. The compiler wraps your statements in a hidden class with a Main method, so the program behaves exactly the same. Only one file in a project can contain top-level statements.

C# Basics Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the code to print a message on its own line.

Console.("Good morning");
Multiple choiceeasy

What ends most statements in C#?

Predict the outputmedium

What does this print?

Console.Write("A");
Console.Write("B");
Console.WriteLine("C");
Multiple choicemedium

Which command builds and runs a C# project?

Multiple choicehard

What do top-level statements in Program.cs replace?

C# Basics Quiz

5 questions to check what you learned.

  1. What platform does C# run on?

    • The JVM
    • Node.js
    • .NET
    • Python
  2. Which game engine uses C# for scripting?

    • Unity
    • Unreal Engine
    • Godot only
    • GameMaker
  3. Is C# case-sensitive?

    • No
    • Only for strings
    • Only on Linux
    • Yes, Console and console are different names
  4. What naming style do C# methods and classes use?

    • camelCase
    • PascalCase
    • snake_case
    • kebab-case
  5. What does "statically typed" mean?

    • Every variable has a type that the compiler checks before the program runs
    • Variables never change
    • Code runs without compiling
    • Only static methods are allowed