Question
C# Class Member Order: Organizing Fields, Properties, Constructors, and Methods
Question
Is there an official C# guideline that specifies the order of members within a class?
For example, should a class always be organized in this order?
public fields
private fields
properties
constructors
methods
Is there a strict rule for ordering fields, properties, constructors, and methods, or should a team choose and consistently follow its own convention?
Short Answer
C# does not require fields, properties, constructors, and methods to appear in a particular order. A consistent ordering convention improves readability and makes classes easier to scan, but the best convention is usually one your team documents and enforces with code reviews and tooling.
Concept
C# class members can generally be declared in any order. The compiler understands a class based on the names, types, and relationships of its members—not on whether a constructor appears before a property.
For example, this is valid even though the constructor uses a field declared later:
public class Counter
{
public Counter(int start)
{
_value = start;
}
private int _value;
}
Although member order does not change correctness, it affects human readability. When every class follows a familiar structure, developers can quickly find:
- dependencies stored in fields,
- public information exposed by properties,
- ways to create the object,
- public behavior, and
- private implementation details.
There is no single mandatory, language-level order. Microsoft’s C# coding conventions discuss many style choices, but a project can reasonably adopt a different member layout when it is documented and consistently applied.
A useful general principle is: organize code so related members are easy to find and understand. Consistency matters more than choosing one supposedly perfect order.
Mental Model
Think of a class as a well-organized instruction manual.
- Fields are the private notes and supplies the class keeps nearby.
- Properties are the labeled controls other code can use.
- Constructors are the setup instructions for creating the object.
- Methods are the actions the object can perform.
A reader should be able to open the manual and locate each category without searching through unrelated pages. The exact table of contents is less important than using the same one throughout the project.
Also, keep closely connected items together. A private helper method may be easier to understand near the public method it supports than in a distant block containing every private method in the class.
Syntax and Examples
A common C# convention is to put the class’s externally useful API near the top and implementation details lower down:
public class BankAccount
{
// Fields
private decimal _balance;
// Properties
public string AccountNumber { get; }
public decimal Balance => _balance;
// Constructor
public BankAccount(string accountNumber, decimal openingBalance)
{
AccountNumber = accountNumber;
_balance = openingBalance;
}
// Public methods
public void Deposit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount));
}
_balance += amount;
}
public bool TryWithdraw(decimal amount)
{
if (amount <= 0 || amount > _balance)
{
;
}
_balance -= amount;
;
}
}
Step by Step Execution
Consider this class layout:
public class Temperature
{
private double _celsius;
public double Celsius => _celsius;
public Temperature(double celsius)
{
_celsius = celsius;
}
public double ToFahrenheit()
{
return (_celsius * 9 / 5) + 32;
}
}
When this code runs:
var temperature = new Temperature(20);
double fahrenheit = temperature.ToFahrenheit();
The execution is:
new Temperature(20)calls the constructor.- The constructor assigns
20to the private_celsiusfield. temperature.ToFahrenheit()calls the public method.- The method reads
_celsius, calculates , and returns .
Real World Use Cases
A predictable member order helps in many everyday C# projects:
- ASP.NET Core controllers: Place injected service fields, the constructor, public action methods, then private helper methods in a recognizable layout.
- Domain models: Put identity and public properties near the top, then domain operations such as
Cancel,Approve, orAddItem. - API clients: Group configuration fields and constructors together, expose public request methods, and keep request-building helpers private.
- Background services: Show dependencies and startup configuration first, followed by the main execution method and private processing helpers.
- Desktop or game applications: Group event declarations, properties, constructors, and event-handler methods consistently so maintenance is faster.
The aim is not merely visual tidiness. Familiar structure reduces the time needed to inspect, review, debug, and modify a class.
Real Codebase Usage
In real codebases, teams usually combine a member-order convention with a few practical patterns.
Keep dependencies and construction visible
Constructor-injected dependencies are commonly placed near the top:
public class OrderService
{
private readonly IOrderRepository _orders;
private readonly ILogger<OrderService> _logger;
public OrderService(IOrderRepository orders, ILogger<OrderService> logger)
{
_orders = orders;
_logger = logger;
}
}
This makes it clear what the class needs to do its work.
Put the public API before implementation details
Callers care about public members. Many teams place public methods first and private helpers afterward:
public class EmailValidator
{
public bool IsValid(string? email)
{
if (string.IsNullOrWhiteSpace(email))
{
return false;
}
return HasAtSign(email) && HasDomain(email);
}
private () => email.Contains();
=> email.EndsWith(, StringComparison.OrdinalIgnoreCase);
}
Common Mistakes
Treating a preferred order as a C# compiler rule
This is false: the compiler does not require constructors, fields, or methods to appear in a fixed sequence.
public class ValidClass
{
public void Run() { }
private int _count;
public ValidClass()
{
_count = 0;
}
}
This compiles. Do not reject code only because it differs from a convention unless your project has explicitly adopted that convention.
Exposing mutable public fields
A layout such as “public fields first” can accidentally normalize public fields. In most application code, prefer properties or methods because they allow validation and can protect object state.
// Usually avoid this.
public decimal Balance;
// Prefer a controlled public view.
public decimal Balance { get; private set; }
Separating related code too aggressively
Putting every private method at the bottom is often fine, but it can make a class difficult to read when a public method depends on several helpers. Keep a coherent group together when that tells the story better.
Inconsistent ordering between files
Comparisons
| Approach | Strengths | Limitations | Good fit |
|---|---|---|---|
| Group by member kind | Easy to scan: fields, properties, constructors, then methods | Related behavior can be far from its helpers | Small and medium application classes |
| Public API first, private details later | Readers see how to use the class immediately | May split fields from the methods that use them | Libraries, services, and reusable components |
| Group by feature or behavior | Keeps a public operation near its private helpers | Categories are less uniform across the file | Classes with several distinct operations |
| Strict analyzer-enforced order | Reduces review debates and keeps files uniform | Can become rigid or require tool configuration | Large teams with established standards |
| No documented convention | Maximum individual freedom | Harder navigation and more style discussion |
Cheat Sheet
- C# has no required compiler-enforced order for class members.
- Choose a team convention and apply it consistently.
- A common layout is:
public class Example
{
// Constants and static fields
// Instance fields
// Constructors
// Properties and events
// Public methods
// Private helper methods
}
- Another valid layout places properties before constructors.
- Prefer private fields plus public properties or methods over mutable public fields.
- Put
readonlydependencies near the constructor that receives them. - Keep related members together when that improves comprehension.
- Use code review,
.editorconfig, and analyzers for shared style rules. - Take care when reordering field initializers with side effects: declaration order may affect behavior.
FAQ
Is there an official required order for members in a C# class?
No. C# allows fields, properties, constructors, methods, and nested types in various orders. The compiler does not impose a general class-member ordering rule.
Should C# fields be public or private?
Most fields should be private. Expose data through properties or methods when outside code needs access, because this preserves control over validation and future changes.
Should constructors come before properties in C#?
Either order is valid. Choose the ordering used by your existing project or team. Consistency is more useful than the specific choice.
Does member order affect C# performance?
Ordinary source ordering does not provide a meaningful performance benefit. It is primarily a readability and maintenance concern.
Can a C# constructor use a field declared later in the file?
Yes. Members of a class are available throughout that class regardless of where their declarations appear. Field initializer order is a separate concern when initializers have side effects.
How can a team enforce C# member ordering?
Document the convention, apply it in code review, and optionally configure IDE tooling or analyzer rules that support the chosen style. The appropriate tool depends on the project’s existing toolchain.
Should private helper methods always be at the bottom of a class?
Often, but not always. Putting them after public methods is a common convention. Keep them close to the feature they support if that makes the code easier to follow.
Mini Project
Description
Create a small TaskList class with a predictable member layout. The class represents a simple in-memory task tracker and demonstrates private state, a public read-only property, construction, public operations, and private validation helpers.
Goal
Build a TaskList class that adds and completes task items while keeping its members organized using one consistent C# convention.
Requirements
Requirement 1
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.