c# - Hide SortedList's .Add method vs using another method name with base.Add -
i have custom class sortedlist few properties , methods. additional processing when new key/value pair added (i.e when .add
method called). can hide .add
method or use method name (ex:.addpair
) , call base.add
in method. preferred approach? why?
hide .add method:
using system; using system.collections.generic; using system.linq; using system.text; namespace inheritence_test { class program { static void main(string[] args) { dyseries d = new dyseries() { fieldname = "test" }; d.add(new datetime(2010, 12, 1), 2345); d.add(new datetime(2010, 12, 5), 2340); d.add(new datetime(2010, 12, 2), 2343); console.writeline("fieldname {0} \n count {1} \n max {2} \n min {3}", d.fieldname, d.count(), d.keys.max(), d.keys.min()); } } class dyseries : sortedlist<datetime, double> { public string fieldname { get; set; } new public void add(datetime date, double value) { base.add(date,value); // additional processing here console.writeline("added date {0}.\n max date: {1}",date, this.keys.max()); } } }
or
using method name:
class program { static void main(string[] args) { dyseries d = new dyseries() { fieldname = "test" }; d.addpair(new datetime(2010, 12, 1), 2345); d.addpair(new datetime(2010, 12, 5), 2340); d.addpair(new datetime(2010, 12, 2), 2343); d.addpair(new datetime(2010, 12, 9), 2348); console.writeline("fieldname {0} \n count {1} \n max {2} \n min {3}", d.fieldname, d.count(), d.keys.max(), d.keys.min()); } } class dyseries : sortedlist<datetime, double> { public string fieldname { get; set; } public void addpair(datetime date, double value) { base.add(date,value); // additional processing here console.writeline("added date {0}.\n max date: {1}",date, this.keys.max()); } }
is there preferred approach? 1 approach (hiding?) potentially cause problems?
use second approach. first breaks oo design, , you're not going sure if method called or base class. consider use of class:
sortedlist<datetime, double> mylist = new dyseries(); mylist.add(date, value); // call base, not implementation!
i've never come across valid reason use new
; there other ways acomplish want without breaking oo design.
Comments
Post a Comment