When you first learn about value types in C#, one of the most interesting features is the default keyword. For structs, using default(T) returns a value where every field is set to its default instance. In this post, we’ll explore what that means, how it works, and what you should keep in mind when designing your own structs.

What Is the default Keyword?

In C#, every type has a default value. For example:

  • default(int) yields 0
  • default(bool) yields false
  • default(string) yields null

For any type T, the expression default(T) returns a value of that type with every field set to its default. With reference types the default is null, but for value types like structs the default value is the result of zeroing out the memory.

Every struct in C# gets an implicit parameterless constructor that “zeroes out” its data—meaning every numeric field becomes 0, every Boolean becomes false, and every reference field becomes null. Consider the following simple struct:

Point p1 = default(Point);
Point p2 = new Point();

public struct Point
{
    public int X;
    public int Y;
}

both p1 and p2 will have X = 0 and Y = 0. The default operator (or the parameterless construction via new Point()) produces an instance with all fields set to their “zero” state.

Constructors and default(T): A Subtle Nuance

Historically, structs couldn’t have a user-defined parameterless constructor (C# 10 introduced the ability to declare one). Even if you write one, the default(T) expression—and array initializations, for example—still use the bitwise-zeroing approach rather than calling your constructor.

For instance, suppose you define:

public struct MyStruct
{
    public int Number;
    public string Text;

    public MyStruct(int number, string text)
    {
        Number = number;
        Text = text;
    }
}

if you call:

MyStruct defaultStruct = default(MyStruct);

you get Number = 0 and Text = null—the bitwise zeroed representation. This means that when designing your struct, you should ensure that the “zero” state is a valid (or at least sensible) state for your type.

Best Practices

  • Valid Default State: Ensure your struct’s “zeroed” state (the default value) is meaningful. If it’s not, you might run into bugs when a struct is implicitly initialized to default.
  • Reference Fields: If your struct contains reference type fields, they will be set to null by default. Make sure your code can handle these nulls appropriately.
  • Custom Constructors vs. default(T): Remember that default(T) bypasses any custom parameterless constructor you might define (in C# 10 and later, even if you write one, default(T) still returns a bitwise zero value in many contexts).

Conclusion

The default keyword is a great feature so try to be cautious when working with custom structs in C#.

Affiliate promo

If you love learning new stuff and want to support me, consider buying a course from Dometrain using this link: Browse courses – Dometrain. Thank you!

Leave a comment

Trending