dotnet / dotnet/csharplang

[Proposal]: `fieldof()`

Open
#9,031 0 comments 0 reactions 1 assignee Claimed by @RikkiGibson View on GitHub
Proposal champion
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

# `fieldof()`

* Specification: https://github.com/dotnet/csharplang/blob/main/proposals/fieldof.md
* Discussion: https://github.com/dotnet/csharplang/discussions/8704
* Related to #8635

## Summary
[summary]: #summary

Allow direct assignment and use of a property's backing field during construction, without having to invoke the setter, via a new `fieldof(Prop)` expression.

```cs
class C
{
public C(DataStore store)
{
this.store = store;

// allows giving an initial value for 'this.Prop'
// without calling 'Store.WritePropToDisk()' thru the setter
fieldof(this.Prop) = store.ReadPropFromDisk();

// 'fieldof()' allows general usage of the field from an initialization context, a la writability of 'readonly' fields.
M(ref fieldof(this.Prop));
}

void Method()
{
// error: 'fieldof' can only be used during initialization
fieldof(this.Prop) = "a";
}

private DataStore store;

public string Prop
{
get => field;
set
{
if (value != field)
{
field = value;
store.WritePropToDisk(value);
}
}
}
}
```

## Design meetings

* https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-01-15.md#fieldof

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.