Alan Alan - 28 days ago 8
C# Question

C# Automatic Properties -- setting defaults

What's the easiest/straight-forward way of setting a default value for a C# public property?

// how do I set a default for this?

public string MyProperty { get; set; }


Please don't suggest that I use a private property & implement the get/set public properties. Trying to keep this concise, and don't want to get into an argument about why that's so much better. Thanks.

Answer

Set the default in your constructor:

this.MyProperty = <DefaultValue>;
Comments