Setting a default value for auto implemented properties

normally you can set a default value for your properties by giving the private property a value.

private string description = "none";
public string Description
{
    get 
    {
        return description;
    }
    set
    {
        description = value;
    }
}

but the auto implemented properties which were introduced with C# 3.0 the syntax does look like this

public string Description { get; set; }

The only way to give auto implemented properties a default value is to set the value in constructor.

Sample:

Constructor for Class with auto implemented propertie with default value

this will give you a default value for your auto-implemented property, without breaking object initializer functionallity.

object initialiser not broken

more informations in the MSDN Auto-Implemented Properties

Please Share us:
Please rate this snippet

Leave a Reply