← All notes

C# properties

Answer: A C# property looks like a field to callers but runs get and set (or init) accessors. Unlike fields, properties are not variables, so they cannot be passed as ref or out.

public class Person {
  public required string FirstName { get; init; }
  public string LastName { get; private set; } = "";
  public string Name => $"{FirstName} {LastName}";   // computed
}

Custom accessors

private int _age;
public int Age {
  get => _age;
  set {
    if (value < 0) throw new ArgumentOutOfRangeException(nameof(value));
    _age = value;
    AgeChanged?.Invoke(this, EventArgs.Empty);
  }
}

A get accessor must return or throw.

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Properties - C#, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Using Properties - C#, CC BY 4.0 (dotnet/docs).

Cite this

Permanent ID: iil:note:b4zntcnj

iinventedlight. 2026. "C# properties." iinventedlight Library, iil:note:b4zntcnj, version of 2026-11-04. https://iinventedlight.com/library/notes/csharp-properties/. License: CC-BY-4.0.

@misc{iil_note_b4zntcnj,
  author = {{iinventedlight}},
  title = {{C\# properties}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/csharp-properties/},
  note = {Permanent ID iil:note:b4zntcnj. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-properties.md.