Avatar ·

Order of elements in a C# class: fields, properties, constructors, methods — is there a standard?

Hi everyone! I'm a beginner C# developer, and I'm troubled by one question. When I write classes, I just add fields, then properties, then constructors and methods in the order they come to my mind. But recently during a code-review, a senior developer made a comment: "Wrong order of elements in the class."

I searched the internet and found many different opinions. Some write: first all private fields, then public properties, then constructors, then methods. Others say: first public members, then protected, then private. Still others sort alphabetically.

Here's an example of my class that was criticized:

public class Employee
{
    public string LastName { get; set; }
    private int _age;
    public Employee(string name) { Name = name; }
    public string Name { get; set; }
    public void PrintInfo() { Console.WriteLine($"{Name}, {_age}"); }
    private int _id;
    public int Age { get { return _age; } set { _age = value; } }