<?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/"
	>

<channel>
	<title>Cale Dunlap &#187; extension methods</title>
	<atom:link href="http://www.caledunlap.com/tag/extension-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.caledunlap.com</link>
	<description>Programmer and hobbyist game developer</description>
	<lastBuildDate>Fri, 20 Aug 2010 08:13:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Helpful Extension Methods to the System.String Class in C#</title>
		<link>http://www.caledunlap.com/2009/10/helpful-extension-methods-to-the-system-string-class-in-c/</link>
		<comments>http://www.caledunlap.com/2009/10/helpful-extension-methods-to-the-system-string-class-in-c/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 10:20:03 +0000</pubDate>
		<dc:creator>Cale</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[extension methods]]></category>

		<guid isPermaLink="false">http://caledunlap.com/blog/2009/10/helpful-extension-methods-to-the-system-string-class-in-c/</guid>
		<description><![CDATA[I&#8217;ve been doing a decent]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a decent amount of string manipulation lately and decided to combine that with my newfound knowledge about .NET extension methods. If you don&#8217;t know what extension methods are, they&#8217;re basically a way to extend a class without needing the source code to that particular class&#8211;hence why I can extend the System.String type by simply writing another class. The key is with the parameters of your methods. Here&#8217;s the basic gist of it:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:050e6608-4d1a-4fda-8b59-b3cc677f09bd" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 500px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> {Your Return Type} {Your Method Name} ( <span style="color:#0000ff">this</span> {Type/Class you want to extend} {Parameter Name} )<br /> {<br /> &#160;&#160;&#160;&#160;&#160;<span style="color:#008000">/* Your method body */</span><br /> }</div>
</p></div>
</p></div>
<pre class="code">&nbsp;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>So the key here is the &#8220;this&#8221; keyword before the type identifier in the parameter list of the method.</p>
<p>I&#8217;ve wrapped all of my extension methods into a single class simply called &#8220;ExtensionMethods&#8221; and added it to my project. The namespace doesn&#8217;t matter, so I haven&#8217;t included it in this example. You can either use the global namespace (though I don&#8217;t recommend it) or just create your own namespace called whatever you want.</p>
<p>Without further adue, here&#8217;s the code:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0271f875-eb62-41fe-a74b-7958d98dab65" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 500px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">ExtensionMethods</span><br />
{<br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> Includes the trailing path delimiter.</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;InputPath&quot;&gt;</span><span style="color:#008000">The input path.</span><span style="color:#808080">&lt;/param&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;returns&gt;</span><span style="color:#008000">The input path including a trailing path delimiter if it doesn&#39;t have one</span><span style="color:#808080">&lt;/returns&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">string</span> IncludeTrailingPathDelimiter(<span style="color:#0000ff">this</span> <span style="color:#0000ff">string</span> InputPath)<br />
&#160;&#160;&#160;&#160;{<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (!InputPath.EndsWith(Path.DirectorySeparatorChar.ToString()) &amp;&amp;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;!InputPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">return</span> InputPath + Path.DirectorySeparatorChar;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">return</span> InputPath;<br />
&#160;&#160;&#160;&#160;}</p>
<p>&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> Asserts the trailing path delimiter.</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;InputPath&quot;&gt;</span><span style="color:#008000">The input path.</span><span style="color:#808080">&lt;/param&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> AssertTrailingPathDelimiter(<span style="color:#0000ff">this</span> <span style="color:#0000ff">string</span> InputPath)<br />
&#160;&#160;&#160;&#160;{<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;InputPath = InputPath.IncludeTrailingPathDelimiter();<br />
&#160;&#160;&#160;&#160;}</p>
<p>&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> Trims the extension from a file name.</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;InputFile&quot;&gt;</span><span style="color:#008000">The input file.</span><span style="color:#808080">&lt;/param&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> TrimExtension(<span style="color:#0000ff">this</span> <span style="color:#0000ff">string</span> InputFile)<br />
&#160;&#160;&#160;&#160;{<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">int</span> start, count;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> ((start = InputFile.LastIndexOf(<span style="color:#a31515">&quot;.&quot;</span>)) &gt; 0)<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;count = InputFile.Length &#8211; start;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;InputFile = InputFile.Remove(start, count);<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br />
&#160;&#160;&#160;&#160;}</p>
<p>&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> Gets a file extension of a filename.</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;/summary&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;param name=&quot;InputFile&quot;&gt;</span><span style="color:#008000">The input file.</span><span style="color:#808080">&lt;/param&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#808080">///</span><span style="color:#008000"> </span><span style="color:#808080">&lt;returns&gt;</span><span style="color:#008000">The file extension</span><span style="color:#808080">&lt;/returns&gt;</span><br />
&#160;&#160;&#160;&#160;<span style="color:#0000ff">public</span> <span style="color:#0000ff">static</span> <span style="color:#0000ff">string</span> FileExtension(<span style="color:#0000ff">this</span> <span style="color:#0000ff">string</span> InputFile)<br />
&#160;&#160;&#160;&#160;{<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">int</span> start, count;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> ((start = InputFile.LastIndexOf(<span style="color:#a31515">&quot;.&quot;</span>)) &gt; 0)<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;count = InputFile.Length &#8211; start;<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">return</span> InputFile.Substring(start, count);<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">return</span> <span style="color:#0000ff">string</span>.Empty;<br />
&#160;&#160;&#160;&#160;}</p>
<p>}</p></div>
</div>
</div>
<pre class="code">&nbsp;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now whenever you use a System.String object, you&#8217;ll be able to simply call these methods as if they were part of the System.String class. IE: string MyFileExtension = MyFilePath.FileExtension();</p>
<p>I&#8217;m sure I&#8217;ll add on to this as I go along.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caledunlap.com/2009/10/helpful-extension-methods-to-the-system-string-class-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
