Default Values and Null
Vaibhav • September 11, 2025
In C#, every variable has a type, and every type has a default value. Understanding what happens when a variable
is not explicitly initialized is essential for writing safe and predictable code. Alongside default values, C#
also introduces the concept of null
- a special keyword that represents the
absence of a value. This article explores both ideas in depth, focusing on how they behave in class fields and
how they differ from regular values. We’ll stay within the concepts introduced so far, without using methods,
control structures, or advanced types.
What is a default value?
A default value is the value automatically assigned to a variable when it is declared but not explicitly
initialized. This only applies to variables declared as fields inside a class. Local variables
- those declared inside methods like Main
- must be initialized before use. The
compiler will show an error if you try to use a local variable without assigning a value.
class Person
{
int age; // default value: 0
bool isActive; // default value: false
string name; // default value: null
}
In this example, the class Person
has three fields: age
, isActive
, and name
. Even though they are not initialized, they still have default values:
int
defaults to0
bool
defaults tofalse
string
defaults tonull
These values are assigned automatically when an object of Person
is created.
You don’t need to set them manually unless you want a different starting value.
Default values apply only to fields in classes. Local variables inside methods must be explicitly initialized before use. This rule helps catch errors early and ensures that your program behaves predictably.
Default values for common types
Each type in C# has a specific default value. Here are some examples:
int number; // 0
bool isReady; // false
char letter; // '\0' (null character)
string message; // null
double price; // 0.0
DateTime date; // 01/01/0001 00:00:00
These values are assigned automatically when the variable is declared as a field in a class. You can rely on them when initializing objects, but it’s often better to assign meaningful values explicitly.
Local variables must be initialized
Unlike fields, local variables inside methods do not receive default values. You must assign a value before using them. If you don’t, the compiler will show an error.
static void Main()
{
int age;
Console.WriteLine(age); // ❌ Error: Use of unassigned local variable
}
To fix this, assign a value before using the variable:
static void Main()
{
int age = 0;
Console.WriteLine(age); // ✅ Output: 0
}
This rule helps prevent bugs caused by unpredictable values. It ensures that every local variable has a known value before it’s used.
Always initialize your variables with meaningful values. Even if the type has a default, setting it explicitly makes your intent clear and your code easier to read.
What is null?
null
is a special keyword in C# that represents the absence of a value. It can
be assigned to reference types like string
, but not to value types like int
or bool
- unless they are made nullable
(covered later).
string name = null;
Console.WriteLine(name); // Output: (blank line)
In this example, name
is a string with no value. When printed, it shows
nothing. This is different from an empty string ""
, which is a valid value with
zero characters.
null
is not the same as ""
. The first
means “no value,” while the second means “empty value.” This
distinction is important when checking for missing data.
Using null in variables
You can assign null
to any reference type. This includes strings, arrays, and
objects. For example:
string message = null;
object data = null;
These variables exist, but they don’t point to any actual data. If you try to use them without checking, your program might crash. We’ll learn how to handle this safely in later chapters.
Checking for null
Before using a variable that might be null
, it’s good practice to check its
value. This helps avoid errors and makes your code more robust.
string name = null;
if (name != null)
{
Console.WriteLine(name);
}
This pattern ensures that name
is only used when it has a value. If it’s null
, the program skips the Console.WriteLine
call.
In modern C#, you can use nullable value types like int?
to
allow null
values for types that normally don’t support them. We’ll explore
this in the next article.
Null in class fields
When you declare a reference type field in a class and don’t initialize it, its default value is null
. This is useful for optional data, but it requires careful handling.
class User
{
string name; // default: null
string email; // default: null
}
When you create a User
object, both name
and
email
will be null
unless you assign values.
You should check these fields before using them to avoid errors.
Null in string interpolation
If you use a null
variable in a string interpolation, it won’t crash - but it
won’t show anything either. This can lead to confusing output.
string name = null;
string message = $"Hello, {name}!";
Console.WriteLine(message); // Output: Hello, !
The output is missing the name. To fix this, check for null
before building the
message:
string name = null;
string message = name != null ? $"Hello, {name}!" : "Hello, guest!";
Console.WriteLine(message); // Output: Hello, guest!
This approach ensures that your program behaves correctly even when data is missing.
Common mistakes and how to avoid them
One common mistake is assuming that all variables have default values. This is only true for fields in classes. Local variables must be initialized before use.
// Incorrect
static void Main()
{
int score;
Console.WriteLine(score); // ❌ Error
}
Another mistake is using null
without checking. This can lead to runtime
errors. Always check for null
before using a reference type.
// Risky
string name = null;
Console.WriteLine(name.Length); // ❌ Error: NullReferenceException
To avoid this, check the variable first:
// Safe
if (name != null)
{
Console.WriteLine(name.Length);
}
Default values in algorithms
In Chapter 1, we introduced algorithms as step-by-step instructions. Default values and null
play a role in these steps - especially when initializing data. For example,
you might start with a default score and update it later:
class Game
{
int score; // default: 0
string playerName; // default: null
}
This setup allows you to create a game object without setting values immediately. You can assign them later as the algorithm progresses.
Summary
In C#, default values are automatically assigned to fields in classes. Each type has a specific default - like
0
for int
, false
for bool
, and null
for reference types like string
. Local
variables inside methods must be initialized before use. The null
keyword
represents the absence of a value and is used with reference types.
You learned how default values behave, how to use null
safely, and how to avoid
common mistakes. These concepts help you write reliable programs that handle missing data gracefully. As we move
forward, you’ll learn how to work with nullable value types and how to design robust systems that deal with
uncertainty.