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#
- Install the .NET SDK from Microsoft's dotnet website. It includes the compiler and the
dotnetcommand. - Create a project and run it from a terminal:
dotnet new console -o HelloApp
cd HelloApp
dotnet rundotnet 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:
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C#");Console.WriteLineprints text followed by a new line;Console.Writeprints 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
// 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:
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 fileThe 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:
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>OutputTypeExemeans this project is a program you can run, rather than a library for other projects.TargetFrameworkpicks the .NET version your code is built for.ImplicitUsingsadds commonusingdirectives for you, includingSystem,System.IO,System.LinqandSystem.Collections.Generic. That's whyConsole.WriteLineworks in a brand-newProgram.csthat has nousing System;line.Nullableturns 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.
Hello, World
Console.WriteLine("Hello, World!");
Console.WriteLine("C# is fun.");Hello, World!
C# is fun.Line-by-line explanation
- Line 1WriteLine prints the text and moves to a new line.
- Line 2Each statement ends with a semicolon.
Write versus WriteLine
Console.Write("Loading");
Console.Write(".");
Console.Write(".");
Console.WriteLine(".");
Console.WriteLine("Done!");
Console.WriteLine();
Console.WriteLine(7 + 3);Loading...
Done!
10Line-by-line explanation
- Lines 1 to 3Write keeps printing on the same line.
- Line 4WriteLine ends the line after printing.
- Line 6WriteLine with nothing prints an empty line.
- Line 7Without quotes, 7 + 3 is a calculation, so it prints 10.
The classic Main method
using System;
namespace Greeter
{
class Program
{
static void Main(string[] args)
{
// Program starts here
Console.WriteLine("Starting the greeter");
Console.WriteLine("Hello from Main");
}
}
}Starting the greeter
Hello from MainLine-by-line explanation
- Line 1using lets you write Console instead of System.Console; new projects add it automatically.
- Line 3A namespace groups related code.
- Line 7Main is the entry point, where the program starts.
- Line 9The comment is ignored by the compiler.
Common C# Basics Mistakes
A missing semicolon
Console.WriteLine("Hello") // error CS1002: ; expectedConsole.WriteLine("Hello");Wrong capitalisation
console.writeline("Hello"); // error CS0103: The name 'console' does not existConsole.WriteLine("Hello");Single quotes for text
Console.WriteLine('Hello'); // error: too many characters in character literalConsole.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.
Complete the code to print a message on its own line.
Console.("Good morning");
What ends most statements in C#?
What does this print?
Console.Write("A");
Console.Write("B");
Console.WriteLine("C");
Which command builds and runs a C# project?
What do top-level statements in Program.cs replace?
C# Basics Quiz
5 questions to check what you learned.
What platform does C# run on?
- The JVM
- Node.js
- .NET
- Python
Which game engine uses C# for scripting?
- Unity
- Unreal Engine
- Godot only
- GameMaker
Is C# case-sensitive?
- No
- Only for strings
- Only on Linux
- Yes, Console and console are different names
What naming style do C# methods and classes use?
- camelCase
- PascalCase
- snake_case
- kebab-case
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