<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>John W Powell : cannot implement an interface member because it is</title><link>http://blogs.msdn.com/johnwpowell/archive/tags/cannot+implement+an+interface+member+because+it+is/default.aspx</link><description>Tags: cannot implement an interface member because it is</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to Implement an Interface Without Making Members Public Using Explicit Interface Implementation</title><link>http://blogs.msdn.com/johnwpowell/archive/2008/06/13/how-to-implement-an-interface-without-making-members-public-using-explicit-interface-implementation.aspx</link><pubDate>Sat, 14 Jun 2008 01:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8595089</guid><dc:creator>johnwpowell</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/johnwpowell/comments/8595089.aspx</comments><wfw:commentRss>http://blogs.msdn.com/johnwpowell/commentrss.aspx?PostID=8595089</wfw:commentRss><description>&lt;P&gt;If you've ever implemented an interface, you've probably encountered the requirement that the member you are implementing must be public.&amp;nbsp; For example, you cannot define a property in an interface and then make the setter internal as demonstrated by the following example:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_4.png"&gt;&lt;IMG height=328 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_1.png" width=327 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;If you attempt to compile the above code, you will receive an error stating that the class does not implement the interface member because it's not public.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_6.png"&gt;&lt;IMG height=81 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_2.png" width=648 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;There are use cases for implementing interfaces and make the members non-public.&amp;nbsp; Building off the example above, &lt;STRONG&gt;suppose I want all entity objects to have a an Id property that anyone can get, but only internal classes can set&lt;/STRONG&gt;.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;I'll walk you through a series of code revisions and solve the problem using &lt;STRONG&gt;explicit interface implementation&lt;/STRONG&gt;.&amp;nbsp; Explicit implementation essentially hides the class member so that it can only be accessed through the interface.&amp;nbsp; To use this feature, prefix the member you are implementing with the interface name, as demonstrated by the Id property below:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_16.png"&gt;&lt;IMG height=244 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_6.png" width=267 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_6.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Now the Id property is no longer visible from the Customer class:&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_10.png"&gt;&lt;IMG height=163 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_4.png" width=328 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;To access an explicitly implemented member, cast it to the interface that contains the member (IEntity in this example):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_12.png"&gt;&lt;IMG height=186 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_5.png" width=345 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;I don't want my consumers to have to cast entity objects to IEntity every time they need to access the Id property, so I'll add a read-only Id property on the class.&amp;nbsp; At first it might feel strange having a member with the same name without using overloading, overriding or hiding (new keyword), but it actually is pretty logical.&amp;nbsp; A class can implement many interfaces, and explicit implementation is the mechanism that handles name clashes when two interfaces define a member with the same name.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_14.png"&gt;&lt;IMG height=327 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb.png" width=271 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although it may appear we have arrived at the solution, we aren't quite there yet.&amp;nbsp; &lt;STRONG&gt;Explicit implementation hides the member, but it does not restrict access to it&lt;/STRONG&gt;; consumers can still cast the object and access the member.&amp;nbsp; To restrict access, we will split non-public members into an interface, and put an appropriate access modifier on the interface.&amp;nbsp; This way consumers cannot cast to that interface and access explicitly implemented members, because they can't access the interface.&amp;nbsp; Let's revisit the example and make some changes.&amp;nbsp; First, the public interface should only contain that which is public, so we'll remove the setter.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_18.png"&gt;&lt;IMG height=79 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_7.png" width=216 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Now we'll move the Id property setter into a new interface that is only visible internally:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_22.png"&gt;&lt;IMG height=82 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_9.png" width=301 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_9.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Next, we'll update the Customer entity to &lt;STRONG&gt;explicitly implement&lt;/STRONG&gt; the IEntityIdSetter interface:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_24.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_24.png"&gt;&lt;IMG height=382 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_10.png" width=402 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_10.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;For illustrative purposes and to demonstrate why we used interfaces in the first place, we'll also implement an Employee class that implements the same interfaces:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_26.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_26.png"&gt;&lt;IMG height=21 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_11.png" width=395 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_11.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Now any class within the assembly can set the Id property and the interface enables us to operate on different types yet treat them as one.&amp;nbsp; In this example, we don't care if it's an Employee or a Customer, just that it supports setting the Id:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_30.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_30.png"&gt;&lt;IMG height=130 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_13.png" width=486 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_13.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;If any class outside the assembly attempts to set the property, the compiler will display an error that the property is read only:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_32.png" mce_href="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_32.png"&gt;&lt;IMG height=95 alt=image src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_14.png" width=525 border=0 mce_src="http://blogs.msdn.com/blogfiles/johnwpowell/WindowsLiveWriter/Howto_E83A/image_thumb_14.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Summary&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Sometimes you need to implement an interface and don't want a member to be public.&amp;nbsp; To achieve this, perform the following steps:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Separate non-public members into an &lt;STRONG&gt;interface&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Set the interface &lt;STRONG&gt;access modifier&lt;/STRONG&gt; to &lt;STRONG&gt;internal&lt;/STRONG&gt; (you can't use private, protected or protected internal)&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Explicitly implement&lt;/STRONG&gt; the non-public interface member(s)&lt;/LI&gt;&lt;/UL&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;References&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/87d83y5b.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/87d83y5b.aspx"&gt;Interface (C# Reference)&lt;/A&gt; 
&lt;LI&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/ms173157(vs.80).aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/ms173157(vs.80).aspx"&gt;Explicit Interface Implementation&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8595089" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/johnwpowell/archive/tags/cannot+implement+an+interface+member+because+it+is+not+public/default.aspx">cannot implement an interface member because it is not public</category><category domain="http://blogs.msdn.com/johnwpowell/archive/tags/Explicit+Interface/default.aspx">Explicit Interface</category><category domain="http://blogs.msdn.com/johnwpowell/archive/tags/Interface/default.aspx">Interface</category></item></channel></rss>