Alan Alan - 1 year ago 106
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 Source

Set the default in your constructor:

this.MyProperty = <DefaultValue>;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download