So I have a base class, which has a property ItemList:
public class BaseClass {
private List<Item> itemList;
public List<Item> ItemList {
set {
itemList = value;
LoadItems();
}
}
public void LoadItems() { ... }
}
And now I have a derived class, in which I want to change the behavior of the function LoadItems(). So I did this:
public class DerivedClass {
public new void LoadItems() { ... }
}
But when I set the property ItemList, only the base LoadItems() is called. Is there anyway I can call the new LoadItems() instead?
Make your property virtual and mark derived class implementation with override, not new: Knowing When to Use Override and New Keywords (C# Programming Guide)
public virtual List<Item> ItemList {
set {
itemList = value;
LoadItems();
}
}
public override List<Item> ItemList {
set {
// do something
// you can also call base.ItemList
// to get base class property implementation
}
}
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