← All notes

C# primary constructors

Answer: The parameters are in scope throughout the type body.

public class OrderService(IOrderRepo repo, ILogger<OrderService> log) {
  public Order Get(int id) {
    log.LogInformation("Get {Id}", id);
    return repo.Find(id);
  }
}

Other constructors must chain

Additional constructors must call the primary one with this(...):

public class Point(int x, int y) {
  public Point() : this(0, 0) {}
  public int X => x;
}

If a class has no explicit constructor, C# supplies a parameterless one; declaring a primary constructor replaces that default.

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), Declare C# primary constructors – classes, structs, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Instance constructors - C#, CC BY 4.0 (dotnet/docs).

Cite this

Permanent ID: iil:note:5wv1yt3d

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

@misc{iil_note_5wv1yt3d,
  author = {{iinventedlight}},
  title = {{C\# primary constructors}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/csharp-primary-constructors/},
  note = {Permanent ID iil:note:5wv1yt3d. 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-primary-constructors.md.