c# - Is an abstract class with fields considered not a good practice? -


i created abstract class implements interface. abstract class base of several concrete classes need populate properties of interface.

the clr compliance warnings pop on first 2 examples. understand represent , there several questions here cover them.

to make field different can add trailing underscore. accepted compiler. correct style choice. don't think stands out , may code smell. may not used it.

or wrong make abstract ancestor defines property fields? idea of course save duplication of work , enforce standard implementation, can see might have own smell in descendent when starts assigning values these "hidden" fields.

namespace mylittlecompany.widgety {   public abstract class mlcwidgetinformation : imlcwidgetinformation   {     //compiler complains of non-clr compliance (case difference only)     protected int sides;  //number of sides widget      //compiler complains of non-clr compliance (non-private name underscore     // not compliant)     protected int _hooks; //number of hooks on widget      //compiler happy trailing underscore     protected int feathers_; //number of feathers on widget      // interface items     public sides { { return sides; } }      public hooks { { return _hooks; } }     public feathers { { return feathers_; } }   } } ===================================== namespace mylittlecompany.widgety {   public class smallwidgetinformation : mlcwidgetinformation    {     public smallwidgetinformation()     {       // smell? in "what these things?"       sides = 6;       _hooks = 3;       feathers_ = 1;      }   } }         

it's absolutely accepted make protected fields in abstract classes.

naming conventions guidelines , depends on styling tool using. use style and/or team preffer , customize tool that. important thing project consistent in own.

i've never seen trailing underscore in use before, can see benefit in it. might pretty smart way show protected fields. definately approve of convention if ran team used it.


Comments