.NET 10 field backed property

chsami - Feb 28 - - Dev Community

With the update to C# 13 Microsoft introduced a new field keyword

Before – The Old Way

You had two choices:

  1. Auto-Implemented Property (No Validation):
   public class TimePeriod
   {
       public double Hours { get; set; }
   }
Enter fullscreen mode Exit fullscreen mode

This creates a compiler-generated backing field, but you can't inject any logic into the get or set accessors.

  1. Manually Implemented Property (With Validation):
   public class TimePeriod
   {
       private double _hours;

       public double Hours
       {
           get { return _hours; }
           set
           {
               if (value < 0)
                   throw new ArgumentOutOfRangeException(nameof(value), "The value must not be negative");
               _hours = value;
           }
       }
   }
Enter fullscreen mode Exit fullscreen mode

Here, you have full control and can add range checking, but you lose the brevity and clarity of auto-properties.

After – With the New field Keyword (C# 13 Preview)

The new contextual keyword field lets you mix the best of both worlds. You can use auto-property syntax and still include custom logic in one accessor without declaring your own backing field:

public class TimePeriod
{
    public double Hours
    {
        get;
        set => field = (value >= 0)
            ? value
            : throw new ArgumentOutOfRangeException(nameof(value), "The value must not be negative");
    }
}
Enter fullscreen mode Exit fullscreen mode

What’s happening here?

  • Auto-Implemented Backing Field: The compiler still creates the backing field for you.
  • Custom Set Logic: You add a validation in the set accessor by directly accessing the synthesized backing field via field.
  • No Redundant Code: There's no need to manually declare the backing field or implement both accessors with full method bodies.

Enjoy!

. . . . . . .