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() //I can just call the empty constructor
{
//do stuff here...
//however, in case of inheritance this would be handled by base(items)
}
So, my question is - can I call both the base
class constructor as well as this
constructor?
Short answer: No, you can't.
Workaround:
public NotificationCollection() : this(Enumerable.Empty<Notification>())
{
}
public NotificationCollection(IEnumerable<Notification> items)
: base(items)
{
this.CollectionChanged += NotificationCollection_CollectionChanged;
this.PropertyChanged += NotificationCollection_PropertyChanged;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With