C# Get Started
Getting started with C# means installing the .NET SDK, creating a project, writing a small program, and running it from the command line or an editor. C# source code is compiled by the .NET tools into an assembly that runs on the Common Language Runtime, so setup matters: the right SDK gives you the compiler, project system, standard libraries, and runtime needed to build real applications.
Overview: How C# Runs
C# is a strongly typed, object-oriented language used for web apps, desktop apps, games, cloud services, mobile apps, and command-line tools. Modern C# normally runs on .NET, an open-source platform that includes the C# compiler, the Base Class Library, and the Common Language Runtime, often called the CLR.
The usual beginner workflow is simple: install the .NET SDK, create a project with dotnet new console, edit Program.cs, run dotnet run, and repeat. A project is more than one code file. It also contains a .csproj file that tells .NET which target framework to use, what dependencies are needed, and how the output should be built.
When you run a C# project, the SDK invokes the C# compiler. The compiler checks your syntax and types, then produces an assembly containing Intermediate Language, or IL, plus metadata describing your types and members. The CLR loads that assembly. At runtime, the just-in-time compiler translates IL into machine code for the current operating system and processor. This is why the same C# source can often run on Windows, macOS, and Linux while still getting native execution speed.
A console app is the best place to start because it removes distractions. You can focus on statements, variables, methods, and types while seeing output immediately in a terminal. Later lessons will add web frameworks, files, collections, classes, and advanced language features, but every C# program still begins with the same basic cycle: write source, compile, run, inspect results.
Syntax
A minimal C# console program has an entry point named Main. In this course, examples use an explicit Program class instead of top-level statements so the structure is always visible.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C#!");
}
}
| Part | Purpose |
|---|---|
using System; |
Makes types in the System namespace available by short name, including Console. |
class Program |
Declares a class named Program. C# code lives inside types such as classes, structs, records, and interfaces. |
static void Main() |
Defines the entry point. static means it belongs to the class, and void means it returns no value. |
Console.WriteLine(...) |
Calls a library method that writes text followed by a newline. |
| Semicolon | Ends a statement. Missing semicolons are a common beginner compile error. |
To create and run a new project from a terminal, use these commands:
dotnet new console -n FirstApp
cd FirstApp
dotnet run
The first command creates a folder and project. The second command enters that folder. The third command restores dependencies if needed, builds the project, and runs the compiled app.
Examples
Example 1: Your First Complete Program
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C#!");
Console.WriteLine("The program is running.");
}
}
Output:
Hello, C#!
The program is running.
This program starts in Main. Each call to Console.WriteLine sends one line to standard output. String text is placed inside double quotes. The order of the output matches the order of the statements because these statements execute sequentially.
Example 2: Variables and Simple Calculation
using System;
class Program
{
static void Main()
{
int lessonsCompleted = 3;
int lessonsPlanned = 10;
int lessonsRemaining = lessonsPlanned - lessonsCompleted;
Console.WriteLine("Completed: " + lessonsCompleted);
Console.WriteLine("Remaining: " + lessonsRemaining);
}
}
Output:
Completed: 3
Remaining: 7
Here the variables are all int, which stores whole numbers. The compiler checks that the subtraction is valid for integers before the program can run. The + operator joins strings with values, so "Completed: " + lessonsCompleted creates one output string.
Example 3: Reading Text and Formatting Output
using System;
class Program
{
static void Main()
{
string courseName = "C#";
int minutesToday = 45;
double hoursToday = minutesToday / 60.0;
Console.WriteLine($"Course: {courseName}");
Console.WriteLine($"Study time: {hoursToday:F2} hours");
}
}
Output:
Course: C#
Study time: 0.75 hours
This example uses string interpolation, which starts with $ before the string. Expressions inside braces are evaluated and inserted into the result. The format F2 prints the number with two digits after the decimal point. The expression uses 60.0 instead of 60 so the division is floating-point division, not integer division.
How It Works Step by Step
- You write C# source code in files ending with
.cs. - The project file, usually ending in
.csproj, says which .NET target framework and options the project uses. dotnet buildcalls the C# compiler. The compiler parses your code, checks names and types, and reports errors before any program runs.- If compilation succeeds, the compiler writes an assembly, usually a
.dll, into abinfolder. dotnet runbuilds if necessary, starts the runtime, loads the assembly, findsMain, and executes it.- While executing, the CLR manages memory, handles exceptions, loads referenced assemblies, and translates IL into native code as needed.
The important point is that many mistakes are caught before runtime. If you write int count = "five";, the compiler rejects it because a string cannot be assigned to an integer. This compile-time checking is one reason C# is productive for larger programs: many errors are found early, close to the line that caused them.
Common Mistakes
Installing Only a Runtime
A runtime can execute existing .NET apps, but it cannot create or compile new C# projects. For development, install the .NET SDK. You can check your installation with:
dotnet --version
If that command prints a version number, the command-line tools are available. If it is not found, install the SDK and reopen your terminal so the updated PATH is loaded.
Forgetting a Semicolon
This code is invalid because the statement is missing its semicolon:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Missing semicolon")
}
}
The corrected version ends the statement with ;:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Semicolon fixed");
}
}
Output:
Semicolon fixed
Confusing Integer and Decimal Division
This program compiles, but the result may surprise you:
using System;
class Program
{
static void Main()
{
int minutes = 45;
double hours = minutes / 60;
Console.WriteLine(hours);
}
}
Output:
0
Both operands in minutes / 60 are integers, so C# performs integer division first and produces 0. Only after that is the result converted to double. Use 60.0 to force floating-point division:
using System;
class Program
{
static void Main()
{
int minutes = 45;
double hours = minutes / 60.0;
Console.WriteLine(hours);
}
}
Output:
0.75
Best Practices
- Install the SDK, not just the runtime, when you plan to write code.
- Start with console applications while learning core language features.
- Use clear project and folder names without spaces when you are new to command-line tools.
- Read compiler errors from the top down. The first error is often the real cause.
- Keep one concept per small test program while practicing; combine concepts after they are clear.
- Use meaningful variable names such as
lessonsRemaininginstead of vague names such asx. - Run your program often. Small changes are easier to debug than a large block of untested code.
- Prefer explicit examples with
class ProgramandMainuntil you understand where execution begins.
Practice Exercises
- Create a console project named
StarterPractice. Change the program so it prints your name, the course name, and one goal for learning C#. - Write a program that stores the number of pages in a book and the number of pages read. Print the number of pages remaining.
- Write a program that converts
90minutes into hours using decimal division. Expected output should include1.5.
Summary
- C# development starts with the .NET SDK, which includes the compiler, runtime, libraries, and project tools.
- A console project is created with
dotnet new consoleand run withdotnet run. - Execution begins in
static void Main()when using the explicit program structure shown in this course. - The compiler produces IL, and the CLR loads it, manages execution, and just-in-time compiles it to native code.
- Many setup and beginner errors are simple: missing SDK, missing semicolons, wrong folder, or integer division when decimal division was intended.
