<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Corey Coogan &#187; partial methods</title>
	<atom:link href="http://blog.coreycoogan.com/tag/partial-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.coreycoogan.com</link>
	<description>.Net, C#, ASP.NET MVC, Architecture and Design</description>
	<lastBuildDate>Sat, 04 Sep 2010 15:17:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.coreycoogan.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/ba1b8857133275abcfb1794dbd2e242e?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Corey Coogan &#187; partial methods</title>
		<link>http://blog.coreycoogan.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.coreycoogan.com/osd.xml" title="Corey Coogan" />
	<atom:link rel='hub' href='http://blog.coreycoogan.com/?pushpress=hub'/>
		<item>
		<title>Partial Methods on Partial Classes</title>
		<link>http://blog.coreycoogan.com/2009/06/09/partial-methods-on-partial-classes/</link>
		<comments>http://blog.coreycoogan.com/2009/06/09/partial-methods-on-partial-classes/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 18:33:15 +0000</pubDate>
		<dc:creator>coreycoogan</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[partial methods]]></category>

		<guid isPermaLink="false">http://blog.coreycoogan.com/?p=56</guid>
		<description><![CDATA[Here&#8217;s something I learned recently while reading Professional ASP.NET 1.0 &#8211; not only are there partial classes, but also partial methods. I can&#8217;t believe this feature has been around as long as partial classes and I haven&#8217;t heard about it until now [UPDATE 7/10/2009: Thanks to Scott Mitchell's Comment, where he pointed out that this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.coreycoogan.com&amp;blog=7719567&amp;post=56&amp;subd=coreycoogan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s something I learned recently while reading <a href="http://http//www.amazon.com/Professional-ASP-NET-MVC-1-0-Conery/dp/0470384611">Professional ASP.NET 1.0</a> &#8211; not only are there partial classes, but also partial methods.</p>
<p>I can&#8217;t believe this feature has been around as long as partial classes and I haven&#8217;t heard about it until now [<em>UPDATE 7/10/2009: Thanks to <a href="#comments">Scott Mitchell's Comment</a>, where he pointed out that this actually came with the 3.5 Framework</em>].  I must have read 25 blog posts and articles when partial classes were being introduced talking about how cool they were and none of them ever mentioned the awesomeness of partial methods.</p>
<p>Until now, when I wanted to define a method on one side of a generated class and let it be implemented on another, I would create a base class and add a virtual method to be overridden, such as the example below.  Partial methods are private, so this approach still has merit when true inheritance is required.</p>
<p><strong>Old Way</strong></p>
<pre class="brush: csharp;">
//My generated class
public partial class DataMapper : DataMapperBase
{
     public void Map()
     {
          //foreach loop or something

         //now do custom mapping
         MapCustom();
     }
}

//my custom partial class
public partial class DataMapper
{
     protected override void MapCustom()
     {
         //do custom mapping
     }
}

//generated base class
public abstract class DataMapperBase
{
     protected virtual void MapCustom(){}
}
</pre>
<p>As I said, this works, but it&#8217;s ugly.  Now here&#8217;s what it looks like with partial methods:</p>
<p><strong>With Partial Methods</strong></p>
<pre class="brush: csharp;">
//My generated class
public partial class DataMapper
{
     public void Map()
     {
          //foreach loop or something

         //now do custom mapping
         MapCustom();
     }

     //define the partial method
     partial void MapCustom();
}

//my custom partial class
public partial class DataMapper
{
     //implement the partial method
     partial void MapCustom()
     {
         //do custom mapping
     }
}
</pre>
<p>How do partial methods work you ask? If a partial method gets implemented by any of the other partials, it&#8217;s compiled and all is good. If it is not implemented, the compiler removes the method definition, as well as all calls to it, as though it was never there. More details are available on the <a href="http://msdn.microsoft.com/en-us/library/wa80x488.aspx">MSDN site</a>.</p>
<p>There are some limitations, so here&#8217;s the rules (from MSDN):</p>
<ul>
<li>Partial method declarations must begin with the contextual keyword <a id="ctl00_MTContentSelector1_mainContentContainer_ctl64" href="http://msdn.microsoft.com/en-us/library/wbx7zzdd.aspx">partial</a> and the method must return <a id="ctl00_MTContentSelector1_mainContentContainer_ctl65" href="http://msdn.microsoft.com/en-us/library/yah0tteb.aspx">void</a>.</li>
<li>Partial methods can have <a id="ctl00_MTContentSelector1_mainContentContainer_ctl66" href="http://msdn.microsoft.com/en-us/library/14akc2c7.aspx">ref</a> but not <a id="ctl00_MTContentSelector1_mainContentContainer_ctl67" href="http://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx">out</a> parameters.</li>
<li>Partial methods are implicitly <a id="ctl00_MTContentSelector1_mainContentContainer_ctl68" href="http://msdn.microsoft.com/en-us/library/st6sy9xe.aspx">private</a>, and therefore they cannot be <a id="ctl00_MTContentSelector1_mainContentContainer_ctl69" href="http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx">virtual</a>.</li>
<li>Partial methods cannot be <a id="ctl00_MTContentSelector1_mainContentContainer_ctl70" href="http://msdn.microsoft.com/en-us/library/e59b22c5.aspx">extern</a>, because the presence of the body determines whether they are defining or implementing.</li>
<li>Partial methods can have <a id="ctl00_MTContentSelector1_mainContentContainer_ctl71" href="http://msdn.microsoft.com/en-us/library/98f28cdx.aspx">static</a> and <a id="ctl00_MTContentSelector1_mainContentContainer_ctl72" href="http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx">unsafe</a> modifiers.</li>
<li>Partial methods can be generic. Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.</li>
<li>You can make a <a id="ctl00_MTContentSelector1_mainContentContainer_ctl73" href="http://msdn.microsoft.com/en-us/library/900fyy8e.aspx">delegate</a> to a partial method that has been defined and implemented, but not to a partial method that has only been defined.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coreycoogan.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coreycoogan.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/coreycoogan.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/coreycoogan.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coreycoogan.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coreycoogan.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coreycoogan.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coreycoogan.wordpress.com/56/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.coreycoogan.com&amp;blog=7719567&amp;post=56&amp;subd=coreycoogan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.coreycoogan.com/2009/06/09/partial-methods-on-partial-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e09ede8123295427210e9a3565804401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coreycoogan</media:title>
		</media:content>
	</item>
	</channel>
</rss>