Design Guidelines Update: Implementing Interfaces Privately


Another one hot off the presses… As always you comments
are welcome.


        style="FONT-WEIGHT: bold">Consider implementing interface members
privately (explicitly), if the members are intended to be called only through
the interface. This includes mainly members supporting some framework
infrastructure, such as data-binding or serialization. For example,
IList<T>.IsReadOnly is intended to be called only by the data-binding
infrastructure though the IList interface. It is almost never called directly on
List<T>.


style="FONT-SIZE: 10pt">public class List<T> : IList<T>
{


style="FONT-SIZE: 10pt">   bool IList<T>.IsReadOnly
{


style="FONT-SIZE: 10pt">         get { …
}


style="FONT-SIZE: 10pt">   }


style="FONT-SIZE: 10pt">}


style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 4pt; BACKGROUND: #e9fdf3; PADDING-BOTTOM: 1pt; MARGIN-LEFT: 0.25in; BORDER-LEFT: windowtext 1pt solid; MARGIN-RIGHT: 0in; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div">

style="BACKGROUND: #e9fdf3; MARGIN-LEFT: 0in"> style="FONT-WEIGHT: bold; FONT-SIZE: 8pt">Note: You need to be
very careful when dealing with value types and privately implemented interfaces.
Casting a value type to an interface, which is the only way to call privately
implemented members, causes boxing.


size=2> face="Times New Roman" size=1> style="FONT: 7pt 'Times New Roman'">           
style="FONT-WEIGHT: bold">Consider implementing interface members
privately to simulate covariance (change type of parameters or/and return value
in “overridden” members). For example, IList implementations often change the
type of the parameters and returned values to create “strongly typed
collections”.


style="FONT-SIZE: 10pt">public class StringCollection : IList
{


style="FONT-SIZE: 10pt">   public string this[int index]{ …
}


style="FONT-SIZE: 10pt">   object IList.this[int index] { …
}


face=Verdana size=2>


style="FONT-SIZE: 10pt">} 


style="MARGIN-LEFT: 0.25in; TEXT-INDENT: -0.25in"> size=2> face="Times New Roman" size=1> style="FONT: 7pt 'Times New Roman'">           
style="FONT-WEIGHT: bold">Avoid implementing interfaces privately
without having a strong reason to do so (see guidelines about infrastructure
interfaces and simulating covariance discussed above). Privately implemented
members can be confusing to developers.


size=2> face="Times New Roman" size=1> style="FONT: 7pt 'Times New Roman'">           
style="FONT-WEIGHT: bold">Do provide protected method that offers
the same functionality as the privately implemented method if the functionality
is meant to be specialized by derived classes. Otherwise it is impossible for
inheritors of the type to call the base method.


style="FONT-SIZE: 10pt">public class List<T> : ISerializable
{


style="FONT-SIZE: 10pt">   …


style="FONT-SIZE: 10pt">   void
ISerializable.GetObjectData(


style="FONT-SIZE: 10pt">      SerializationInfo info,
StreamingContext context) {


style="FONT-SIZE: 10pt">     
GetObjectData();


style="FONT-SIZE: 10pt">   }


style="FONT-SIZE: 10pt"> 


style="FONT-SIZE: 10pt">   protected virtual void
GetObjectData(


style="FONT-SIZE: 10pt">      SerializationInfo info,
StreamingContext context) {


style="FONT-SIZE: 10pt">     


style="FONT-SIZE: 10pt">   }


style="FONT-SIZE: 10pt">}


style="FONT-SIZE: 10pt"> 

Comments (0)