Sunday, 15 April 2012

c# - Is it possible to (partially) implement an abstract method and still require derived classes to also implement it? -



c# - Is it possible to (partially) implement an abstract method and still require derived classes to also implement it? -

here's prototype of i'd do, except realize can't work way i've written it:

using system.collections.objectmodel; namespace merlinia.commonclasses { public abstract class justtesting<titem> : keyedcollection<string, titem> { protected override string getkeyforitem(titem anitem) { homecoming getkeyforitem(anitem).toupperinvariant(); } protected new abstract string getkeyforitem(titem anitem); } }

now realize changing name of abstract method require in derived classes work:

using system.collections.objectmodel; namespace merlinia.commonclasses { public abstract class justtesting<titem> : keyedcollection<string, titem> { protected override string getkeyforitem(titem anitem) { homecoming newgetkeyforitem(anitem).toupperinvariant(); } protected abstract string newgetkeyforitem(titem anitem); } }

it's i'd prefer method name same, getkeyforitem, in of classes. there way create work?

you can insert class , internal helper function in hierarchy this.

using system.collections.objectmodel; namespace merlinia.commonclasses { public abstract class justtestingbase<titem> : keyedcollection<string, titem> { internal justtestingbase() { // other assemblies cannot misuse own base of operations class } protected sealed override string getkeyforitem(titem anitem) { homecoming getkeyforitemhelper(anitem).toupperinvariant(); } internal abstract string getkeyforitemhelper(titem anitem); } public abstract class justtesting<titem> : justtestingbase<titem> { protected new abstract string getkeyforitem(titem anitem); internal override string getkeyforitemhelper(titem anitem) { homecoming getkeyforitem(anitem); } } }

c# abstract-methods

No comments:

Post a Comment