C# field Keyword
Last modified April 19, 2025
This tutorial explores how to use the field
keyword in C# to
declare backing fields for auto-implemented properties.
The field
keyword, introduced in C# 12, provides a way to
explicitly reference the backing field of an auto-implemented property,
enabling custom logic in property accessors while maintaining concise syntax.
Understanding the field Keyword
In C#, auto-implemented properties automatically create a private backing
field. Before C# 12, developers had no direct way to access this field in
custom getter or setter logic. The field
keyword addresses this
by allowing explicit reference to the backing field.
Key characteristics of field
:
- Only usable within property or indexer accessors.
- Refers to the compiler-generated backing field of an auto-implemented property.
- Enables custom logic without manually declaring a backing field.
- Not a standalone variable; it's a contextual keyword.
Basic field Usage
This example shows how to use field
to validate a property value.
class Person { private string _name = "Unknown"; public string Name { get => field; set => field = string.IsNullOrEmpty(value) ? "Unknown" : value; } } var person = new Person(); person.Name = ""; Console.WriteLine(person.Name); person.Name = "Alice"; Console.WriteLine(person.Name);
The Name
property uses field
to ensure the backing
field is never set to an empty or null string.
$ dotnet run Unknown Alice
Property with Custom Getter Logic
This example uses field
to modify the getter of a property.
class Product { public decimal Price { get => field * 1.1m; // Apply 10% markup set => field = value < 0 ? 0 : value; } } var product = new Product(); product.Price = 100; Console.WriteLine(product.Price); product.Price = -50; Console.WriteLine(product.Price);
The Price
property uses field
to apply a markup in
the getter and prevent negative values in the setter.
$ dotnet run 110 0
Using field with Indexers
This example demonstrates field
in an indexer.
class Scores { public int this[int index] { get => field; set => field = value >= 0 && value <= 100 ? value : throw new ArgumentException("Score must be between 0 and 100."); } } var scores = new Scores(); scores[0] = 85; Console.WriteLine(scores[0]); try { scores[1] = 150; } catch (ArgumentException e) { Console.WriteLine(e.Message); }
The indexer uses field
to validate scores, ensuring they are
within a valid range.
$ dotnet run 85 Score must be between 0 and 100.
Combining field with Init-Only Properties
This example shows field
with an init-only property.
class Configuration { public string ConnectionString { get => field; init => field = string.IsNullOrWhiteSpace(value) ? throw new ArgumentException("Invalid connection string") : value; } } var config = new Configuration { ConnectionString = "Server=localhost" }; Console.WriteLine(config.ConnectionString); try { var invalidConfig = new Configuration { ConnectionString = " " }; } catch (ArgumentException e) { Console.WriteLine(e.Message); }
The ConnectionString
property uses field
to enforce
valid initialization.
$ dotnet run Server=localhost Invalid connection string
Using field for Lazy Initialization
This example demonstrates field
for lazy initialization in a
property.
class DataCache { public string Data { get => field ??= LoadData(); set => field = value; } private string LoadData() => "Loaded from source"; } var cache = new DataCache(); Console.WriteLine(cache.Data); cache.Data = "Custom data"; Console.WriteLine(cache.Data);
The Data
property uses field
to lazily initialize
the backing field when first accessed.
$ dotnet run Loaded from source Custom data
Best Practices
When using the field
keyword:
- Use
field
only when custom logic is needed in accessors. - Keep accessor logic simple to maintain readability.
- Combine with validation to enforce business rules.
- Avoid overusing
field
when standard auto-implemented properties suffice.
Source
field keyword - language reference
This tutorial has demonstrated how to use the field
keyword in
C# to work with backing fields of auto-implemented properties and indexers.
Author
List all C# tutorials.