SharePoint: Setting DefaultValue of a WebPart Property

The DefaultValue attribute is used to set the default value of a WebPart property in SharePoint. However, one should remember to use regular properties instead of automatic properties. The Default value is not saved if automatic properties are used. The following code uses automatic properties to save the Default value. Here the value won't be saved.
private const int const_NumberOfFeedItems = 5;
[WebBrowsable(true),
WebDisplayName("Number of feed items"),
WebDescription("Number of feed items"),
Personalizable(PersonalizationScope.Shared),
DefaultValue(const_NumberOfFeedItems),
Category("RSS Feed Custom Properties")]
public int NumberOfItems
{
    get; set; 
}
Following code used Regular properties to save the Default value. Here the value will be saved properly.
private const int const_NumberOfFeedItems = 5;
private int _numberOfFeedItems = const_NumberOfFeedItems;
[WebBrowsable(true),
WebDisplayName("Number of feed items"),
WebDescription("Number of feed items"),
Personalizable(PersonalizationScope.Shared),
DefaultValue(const_NumberOfFeedItems),
Category("RSS Feed Custom Properties")]
public int NumberOfItems
{
    get
    {
         return _numberOfFeedItems;
    }
    set
    {
         _numberOfFeedItems = value;
    }
}

profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+