Friday, 6 September 2013

Calling both a base constructor and a parameterless constructor?

Calling both a base constructor and a parameterless constructor?

Here's something that struck me, and I'm wondering if this is at all
possible.
To make a long story short - here's the code:
public class NotificationCollection : ObservableCollection<Notification>
{
public NotificationCollection() : base()
{
this.CollectionChanged += NotificationCollection_CollectionChanged;
this.PropertyChanged += NotificationCollection_PropertyChanged;
}
public NotificationCollection(IEnumerable<Notification> items)
: base(items)
{
this.CollectionChanged += NotificationCollection_CollectionChanged;
this.PropertyChanged += NotificationCollection_PropertyChanged;
}
(....)
}
As you can see, I'm duplicating code. Had I not been creating an inherited
class, I'd write
public NotificationCollection(IEnumerable<Notification> items)
: this()
{
//do stuff here...
}
So, my question is - can I call both the base class constructor as well as
this constructor?

No comments:

Post a Comment