ProAnswers.org

C# Auto-Property a default value?

How do you give a C# Auto-Property a default value? I either use the constructor, or revert to the old syntax.

Using the Constructor:

class Person
{
public Person()
{ Name = “Default Name”; }
public string Name
{
get; set;
} } Using normal property syntax (with a default value)

private string name = “Default Name”;
public string Name { get
{ return name; }
set { name = value; } }
Is there a better way?

To give auto implemented properties a default value, you’d have to do it in the constructor.