When learning C#, you will often hear two terms:
- Compile-time error
- Runtime error
Both are errors, but they happen at different stages of your program.
Understanding the difference is important because it helps you debug code faster and also helps you understand concepts such as var, dynamic, type checking, exceptions, and compiler warnings.
What is Compile Time?
Compile time is the stage when C# checks your code before the program starts running.
The compiler verifies things such as:
- syntax
- variable types
- method names
- property names
- missing symbols
- invalid assignments
If the compiler finds an invalid statement, your program usually cannot build successfully.
For example:
int age = "25";This is a compile-time error.
Why?
Because:
"25" → string
age → intC# knows that a string cannot be directly assigned to an int.
The compiler catches the problem before the application runs.
Another Compile-Time Error Example
int number = 10;
Console.WriteLine(number.Length);This also causes a compile-time error.
number is an int, and int does not have a Length property.
The compiler already knows the type of number, so it can detect the problem immediately.
What is Runtime?
Runtime is the period when your application is actually executing.
Sometimes code is syntactically and logically valid enough to compile, but an error occurs when a particular line runs.
That is called a runtime error.
For example:
int a = 10;
int b = 0;
int result = a / b;This code can compile successfully.
However, when this line executes:
int result = a / b;the program attempts to divide by zero.
That causes a runtime exception.
Runtime Error with null
Consider this example:
string? name = null;
Console.WriteLine(name.Length);Depending on nullable reference type settings, the compiler may warn you that name can be null.
But the program may still compile.
If this line executes while name is null, the program can throw a:
NullReferenceExceptionThis happens during runtime.
Compile-Time Error vs Runtime Error
| Compile-Time Error | Runtime Error |
|---|---|
| Happens before the program runs | Happens while the program is running |
| Found by the compiler | Happens during execution |
| Program usually cannot build | Program can build successfully |
| Often caused by syntax or type problems | Often caused by invalid data or unexpected conditions |
| Usually easier to catch early | May only happen for certain inputs |
Example: Compile-Time Type Checking
C# is strongly typed.
That means the compiler tries to catch many mistakes before your program runs.
For example:
int marks = 90;
marks = "Ninety";This is invalid.
The compiler already knows:
marks → intSo assigning a string causes an error before execution.
Example: Runtime Input Problem
Now imagine:
string input = "abc";
int number = int.Parse(input);This code can compile.
But when it runs, C# attempts to convert:
"abc"into an integer.
That is not valid, so a runtime exception can occur.
A safer version would be:
string input = "abc";
if (int.TryParse(input, out int number))
{
Console.WriteLine(number);
}
else
{
Console.WriteLine("Invalid number");
}This prevents the exception by checking whether conversion is possible.
Connection with var
The difference becomes very clear when working with var.
Example:
var x = 10;The compiler infers:
x → intSo this causes a compile-time error:
Console.WriteLine(x.Length);Why?
Because the compiler already knows that x is an int.
And int does not have a Length property.
Connection with dynamic
Now consider:
dynamic x = 10;
Console.WriteLine(x.Length);This can compile.
With dynamic, many member checks are postponed until runtime.
When the line actually executes, C# checks the real value stored in x.
It discovers:
x contains an intand an int does not have a Length property.
So the program fails at runtime.
This is a good example of the difference between compile-time and runtime checking.
Syntax Errors Are Usually Compile-Time Errors
For example:
int age = 25The semicolon is missing.
The compiler catches this before execution.
Another example:
if (age > 18
{
Console.WriteLine("Adult");
}The closing parenthesis is missing.
Again, this is caught during compilation.
Common Runtime Errors
Some common runtime exceptions in C# include:
NullReferenceException
DivideByZeroException
IndexOutOfRangeException
FormatException
InvalidCastException
FileNotFoundExceptionFor example:
int[] numbers = { 10, 20, 30 };
Console.WriteLine(numbers[5]);The code compiles.
But the array only has indexes:
0
1
2So accessing index 5 causes an:
IndexOutOfRangeExceptionat runtime.
Why Compile-Time Errors Are Better
Compile-time errors are usually easier to fix because the compiler catches them before your application reaches users.
For example:
int age = "25";is immediately rejected.
You do not need to wait for the application to crash.
This is one reason strong typing is valuable.
It catches many bugs early.
Runtime Errors Are Often Data-Dependent
Runtime errors may only appear for specific situations.
For example:
int result = a / b;works when:
b = 2but fails when:
b = 0So runtime errors can be harder to reproduce because they depend on actual data and execution flow.
How Can We Reduce Runtime Errors?
You can reduce runtime errors by validating data before using it.
For example:
if (b != 0)
{
Console.WriteLine(a / b);
}
else
{
Console.WriteLine("Cannot divide by zero");
}For null values:
if (name != null)
{
Console.WriteLine(name.Length);
}For parsing:
if (int.TryParse(input, out int value))
{
Console.WriteLine(value);
}Good validation prevents many runtime exceptions.
Easy Way to Remember
Think of it like this:
Write code
↓
Compile
↓
Compile-time error?
↓
Fix it
↓
Run program
↓
Runtime error?So:
Compile-time error = discovered before execution
Runtime error = happens during execution
Final Summary
Compile-time and runtime errors are both important concepts in C#.
A compile-time error is detected by the compiler before the application runs.
A runtime error happens while the program is already executing.
The compiler can catch problems such as:
wrong data type
invalid property
syntax error
missing methodBut runtime problems often depend on actual execution, such as:
division by zero
null values
invalid indexes
invalid user input
missing filesUnderstanding this difference also makes concepts like var and dynamic much easier.
A simple rule to remember is:
Compile time → before running
Runtime → while runningThe earlier an error is detected, the easier it usually is to fix.