Properties

  • DO use PascalCasing.

  • DO name properties using a noun, noun phrase, or adjective.

  • ✅ For single line read-only properties, prefer expression body properties (⇒) when possible.

    • For everything else, use the older { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName => $"{FirstName} {LastName}";
    
  • DO always use properties instead of public fields.

    Reason behind this is, it makes your code properly encapsulated in OOPs environment. By using getters & setters, you can restrict the user directly accessing the member variables. You can restrict setting the values explicitly thus making your data protected from accidental changes. Also, properties give you easier validation for your data.

  • CONSIDER giving a property the same name as its type.

    For example, the following property correctly gets and sets an enum value named Color, so the property is named Color:

    public enum Color {...}
    public class Control {
        public Color Color { get {...} set {...} }
    }
    
  • DO NOT have properties that match the name of “Get” methods as in the following example:

    public string TextWriter { get {...} set {...} }
    public string GetTextWriter(int value) { ... }
    

Learn More