DEV Community

Cover image for đź§  Backing Fields in C#: What They Are and Why You Should Care
Jose Rodriguez Marrero
Jose Rodriguez Marrero

Posted on

đź§  Backing Fields in C#: What They Are and Why You Should Care

As a C# developer, you've probably written something like this:

public string Name { get; set; }
Enter fullscreen mode Exit fullscreen mode

Short. Sweet. Gets the job done.

But what if you need to sneak in a little logic? What if you want to say, “Hey! You can't set Age to -200!”?

That’s when it’s time to meet your new(ish) best friend: backing fields.


🔙 Wait… What’s a Backing Field?

A backing field is just a private variable that stores the actual value for a property.

Think of it like this:

// Backing field
private string _email;

// Property using that field
public string Email
{
    get => _email;
    set => _email = value;
}

Enter fullscreen mode Exit fullscreen mode

You're basically wrapping that private _email in a nice public interface (Email) so other parts of your code can use it safely.


✨ Auto-Properties: The Lazy Magic

When you write this:

public string Email { get; set; }
Enter fullscreen mode Exit fullscreen mode

The compiler quietly creates something like:

private string _email; // đź‘» invisible to you!

public string Email
{
    get => _email;
    set => _email = value;
}

Enter fullscreen mode Exit fullscreen mode

No muss, no fuss — but also no customization. You can’t sneak in any logic. It’s like ordering black coffee when you really need a vanilla oat milk latte with light foam and a shot of espresso (a.k.a. validation).


đź’ˇ So When Do You Need a Backing Field?

Glad you asked. Here are a few classic cases:

1. Validation

charp

private int _age;

public int Age
{
    get => _age;
    set => _age = value < 0 ? 0 : value; // No negative ages allowed!
}

Enter fullscreen mode Exit fullscreen mode

2. Logging or Side Effects

private string _name;

public string Name
{
    get => _name;
    set
    {
        Console.WriteLine($"Changing name from {_name} to {value}");
        _name = value;
    }
}

Enter fullscreen mode Exit fullscreen mode

3. Transforming Input

Here’s a real-world example from a product filter class I'm currently building:

private List<string> _brands = [];

public List<string> Brands
{
    get => _brands;
    set => _brands = [.. value.SelectMany(p => p.Split(',', StringSplitOptions.RemoveEmptyEntries))];
}
Enter fullscreen mode Exit fullscreen mode

Now, instead of forcing users to send ["Nike", "Adidas"], they can just send:

new List<string> { "Nike,Adidas", "Puma" }
Enter fullscreen mode Exit fullscreen mode

And you’ll still end up with:

["Nike", "Adidas", "Puma"]
Enter fullscreen mode Exit fullscreen mode

That's right, homeys!!✨


Quick Comparison

Feature Auto-Property Backing Field
Super simple ✅ Yup ❌ More typing
Add logic or validation ❌ Nope ✅ Totally
Trigger side effects ❌ Not a chance ✅ All day
Transform data on set ❌ Can't do it ✅ Yes, chef

đź§µ Wrap-up

Backing fields are like the secret sauce behind well-behaved properties. Sure, auto-properties are great for the everyday stuff, but when you need more control, don’t be afraid to write things out manually.

Whether you're validating data, formatting input, or just want to sneak a Console.WriteLine in there — backing fields have your back (literally).


What’s your favorite use case for a backing field? Ever tried doing too much with an auto-property and had it backfire? Drop a comment — I’d love to hear your war stories.

Top comments (0)