I'm wondering what is the cleanest way to make a property return the value specified in its DefaultValueAttribute if not initialized.
For example, I would like the getter of this property to return 42 if not initialized:
[DefaultValue(42)]
public int MyProperty { get; set; }
Any solution / refactored code welcomed. Target is .Net 4 (if greater, please indicate a fallback)
Ultimately, the aim is to avoid "hardcoding" the default value twice in order to facilitate maintenance. Also note that in a real scenario, there would be several such properties in one class. So ideally, I would prefer to avoid a solution that manually lists and initializes each properties to its default value.