In C#, setting default values for properties has evolved significantly across different versions of the language. Depending on your version of .NET, you can use several clean, readable approaches.
1. Auto-Implemented Property Initializers (C# 6.0+)
This is the most common and modern way to set a default value. It allows you to assign the value directly on the same line where the property is defined.
2. Using the Constructor
If your default value depends on logic, parameters, or you are using an older version of C#, the constructor is the standard place to initialize properties.
3. Required Properties (C# 11+)
Sometimes you don't want a "default" value, but rather to force the caller to provide one. The required keyword ensures the property is set during object initialization.
4. The DefaultValue Attribute (Metadata Only)
It is a common misconception that the [DefaultValue] attribute sets the value of a property. It does not. It is primarily used by visual designers (like WinForms or XAML) or documentation tools to communicate what the default should be.
5. Expression-Bodied Members (Read-Only)
If you want a property to always return a specific value (and never change), you can use the expression-bodied syntax.
Which one should you use?
Use Property Initializers for simple, static values (most common).
Use Constructors if the initialization logic is complex or requires parameters.
Use
requiredif there is no sensible default and the user must provide data.
