i have some questions when watching this tutorial.
i wonder when i overwrite methods, how do i know if i need to call the base method?
public开发者_JS百科 CustomerCollection(IEnumerable<Customer> customers, OMSEntities context) : base(customers)
also why do i need to do
protected override void InsertItem(int index, Customer cust)
{
this.context.AddToCustomers(cust);
base.InsertItem(index, cust);
}
protected override void RemoveItem(int index)
{
this.context.DeleteObject(this[index]);
base.RemoveItem(index);
}
what does the 2 lines in each method do? and why the need for such similar method. if i overwrite methods for delete and add why not update too?
You call the base method when you're method is just additional functionality decorating what the base method does, you don't call the base method when you are replacing it's functionality wholesale.
And if you don't know what the base method does, you wouldn't be overriding it, so in knowing what it does and why you're overriding it, you should be able to make this decision in accordance.
For the first part of your question if you do not add a :base(parameter)
to the end of a constructor it will actually put a :base()
there for you behind the scenes. if there is no base()
you will get a compile exception. you need a chain of constructors all the way down to object()
. adding that :base(parameter)
is just a way of choosing a different constructor other than the default one.
For the second part. It is a good rule of thumb that if you are overriding a method to provide some kind of additional functionality you should call the base method instead too (so you get its functionality), if you are trying to replace the functionality instead of add to it you do not need to call the base(as you are replacing it :) ).
精彩评论