<?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>Tobin Titus &#187; extensions</title>
	<atom:link href="http://tobint.com/blog/tag/extensions/feed/" rel="self" type="application/rss+xml" />
	<link>http://tobint.com</link>
	<description>Abstract Syntax</description>
	<lastBuildDate>Wed, 08 Sep 2010 08:02:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Indexers as Extension Methods?</title>
		<link>http://tobint.com/blog/indexers-as-extension-methods/</link>
		<comments>http://tobint.com/blog/indexers-as-extension-methods/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 09:53:36 +0000</pubDate>
		<dc:creator>tobint</dc:creator>
				<category><![CDATA[technical]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[extensions]]></category>

		<guid isPermaLink="false">http://tobint.com/blog/indexers-as-extension-methods/</guid>
		<description><![CDATA[
			
				
			
		
So I’ve had this nagging issue for a little while. It’s not necessarily a huge issue because I have a workaround, but that said, it still nags at me now and again. That issue is that I cannot create an indexer as an extension method. This isn’t possible for a number of reasons that make [...]


Related posts:<ol><li><a href='http://tobint.com/blog/more-string-stringbuilder-quirks/' rel='bookmark' title='Permanent Link: More String / StringBuilder Quirks'>More String / StringBuilder Quirks</a></li>
<li><a href='http://tobint.com/blog/string-format-vs-stringbuilder-appendformat/' rel='bookmark' title='Permanent Link: String.Format vs StringBuilder.AppendFormat'>String.Format vs StringBuilder.AppendFormat</a></li>
<li><a href='http://tobint.com/blog/exploit-using-system-string-not-really/' rel='bookmark' title='Permanent Link: Exploit using System.String? Not Really'>Exploit using System.String? Not Really</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftobint.com%2Fblog%2Findexers-as-extension-methods%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftobint.com%2Fblog%2Findexers-as-extension-methods%2F&amp;source=tobint&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>So I’ve had this nagging issue for a little while. It’s not necessarily a huge issue because I have a workaround, but that said, it still nags at me now and again. That issue is that I cannot create an indexer as an extension method. This isn’t possible for a number of reasons that make sense, but I thought I’d blog about it anyway and solicit thoughts on the idea.</p>
<p>First, let’s talk about what we can do. I can create an extension method for any existing class. Let’s say I have the following class already created:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> SomeClass{
   <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> SomeProperty { get; set; }
}</pre>
<p> </p>
<p>I can then extend a List of SomeClass pretty easily like this:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> Extensions{
   <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span> SomeListExtensionProperty(<span style="color: #0000ff;">this</span> List&lt;SomeClass&gt; classes)   {
      <span style="color: #008000;">//… provide implementation here …
</span>   }
}</pre>
<p> </p>
<p>Notice that I’ve scoped my extension to only extend a generic List of SomeClass. For instance I <strong>can</strong> do this:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var sc = <span style="color: #0000ff;">new</span> List&lt;SomeClass&gt;();<span style="color: #0000ff;">string</span> s = sc.SomeListExtensionProperty();</pre>
<p> </p>
<p>but I <strong>cannot</strong> do this:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var soc = <span style="color: #0000ff;">new</span> List&lt;SomeOtherClass&gt;();
<span style="color: #0000ff;">string</span> s = soc.SomeListExtensionProperty();</pre>
<p> </p>
<p>So what would happen if I tried to extend the List of SomeClass with an indexer like this:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span> <span style="color: #0000ff;">this</span>[<span style="color: #0000ff;">int</span> index](<span style="color: #0000ff;">this</span> List&lt;SomeClass&gt; classes){
 <span style="color: #0000ff;">return</span> classes[i].SomeProperty;
}</pre>
<p> </p>
<p>We wouldn’t get past compilation. First of all, the word “<span style="text-decoration: underline;">this</span>” and “<span style="text-decoration: underline;">static</span>” don’t mix or tend to make sense together in most cases. That is because “<span style="text-decoration: underline;">this</span>” refers to an instance of a class while “<span style="text-decoration: underline;">static</span>” refers to type itself. In general, mixing these two wouldn’t make sense. That said, we already mix these two keywords when we create extension methods. So that isn’t the only reason this wouldn’t work. The next reason is that the generic List class already provides an indexer. Since you can’t override existing members with extensions, you are left without the ability to create an indexer with an extension.</p>
<p>Our only recourse would be to provide extension methods that provide the same functionality as a method.</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span> GetSomeProperty(<span style="color: #0000ff;">this</span> List&lt;SomeClass&gt; classes, <span style="color: #0000ff;">int</span> index){
   <span style="color: #0000ff;">return</span> classes[index].SomeProperty;
}</pre>
<p> </p>
</div>
</div>
</div>
<p>We can then call something like this:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var sc = <span style="color: #0000ff;">new</span> List&lt;SomeClass&gt;();
<span style="color: #0000ff;">string</span> s = sc.GetSomeProperty(index);</pre>
<p> </p>
<p>This isn’t quite as abbreviated as an indexer and in fact doesn’t save me anything over what I would get with the out-of-the-box generic List indexer:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var sc = <span style="color: #0000ff;">new</span> List&lt;SomeClass&gt;();
<span style="color: #0000ff;">string</span> s = sc[index].SomeProperty;</pre>
<p> </p>
<p>That said, the “nagging issue” is more of a request for a solution looking for a problem. Obviously indexers are great shorthand that ‘can’ provide a ‘default property’ so-to-speak. However, it is very easy to get what you want without much work.</p>
</div>
</div>
</div>
</div>
</div>


<p>Related posts:<ol><li><a href='http://tobint.com/blog/more-string-stringbuilder-quirks/' rel='bookmark' title='Permanent Link: More String / StringBuilder Quirks'>More String / StringBuilder Quirks</a></li>
<li><a href='http://tobint.com/blog/string-format-vs-stringbuilder-appendformat/' rel='bookmark' title='Permanent Link: String.Format vs StringBuilder.AppendFormat'>String.Format vs StringBuilder.AppendFormat</a></li>
<li><a href='http://tobint.com/blog/exploit-using-system-string-not-really/' rel='bookmark' title='Permanent Link: Exploit using System.String? Not Really'>Exploit using System.String? Not Really</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://tobint.com/blog/indexers-as-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
