Looking for Feedback About Extension Methods ArticleHello,<br><br>     I've been thinking about the best ways to use extension methods,  and I've written an article based on what I know at<br><br><a href="http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/">http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/</a><br><br>     There's some stuff that I think I understand pretty well,  but there still are some things that I find murky in the C# 3.0 specification.  I'm looking for general feedback about the article,  but I'm particularly interested in finding more details about how precedence works,  particularly when there are conflicting &quot;using&quot; directives.  For instance,  does the compiler give an error in that case,  or does it just do something automatically?<br><br>     Thanks for any information you can give me.<br><br> © 2009 Microsoft Corporation. All rights reserved.Tue, 08 Jul 2008 20:02:50 Ze42f1511-39e7-4fed-9e56-0cc19c00d33dhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/e42f1511-39e7-4fed-9e56-0cc19c00d33d#e42f1511-39e7-4fed-9e56-0cc19c00d33dhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/e42f1511-39e7-4fed-9e56-0cc19c00d33d#e42f1511-39e7-4fed-9e56-0cc19c00d33dPaul Houlehttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20HouleLooking for Feedback About Extension Methods ArticleHello,<br><br>     I've been thinking about the best ways to use extension methods,  and I've written an article based on what I know at<br><br><a href="http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/">http://gen5.info/q/2008/07/03/extension-methods-nulls-namespaces-and-precedence-in-c/</a><br><br>     There's some stuff that I think I understand pretty well,  but there still are some things that I find murky in the C# 3.0 specification.  I'm looking for general feedback about the article,  but I'm particularly interested in finding more details about how precedence works,  particularly when there are conflicting &quot;using&quot; directives.  For instance,  does the compiler give an error in that case,  or does it just do something automatically?<br><br>     Thanks for any information you can give me.<br><br> Thu, 03 Jul 2008 17:04:16 Z2008-07-03T17:04:16Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/e42f1511-39e7-4fed-9e56-0cc19c00d33d#e6a6907e-f722-40ad-8aa9-9453fbd2ea5ahttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/e42f1511-39e7-4fed-9e56-0cc19c00d33d#e6a6907e-f722-40ad-8aa9-9453fbd2ea5aMatt Manelahttp://social.msdn.microsoft.com/Profile/en-US/?user=Matt%20ManelaLooking for Feedback About Extension Methods ArticleWell, the easiest way too answer the precedence question is to do a little example:<br><b>Program#1</b><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="border-right:1px solid gray;font-family:Courier New;font-size:11px;background-color:rgb(238, 238, 238);padding-right:5px;padding-left:10px;width:5px;color:gray;text-align:right;vertical-align:top"><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><nobr>1</nobr></td><td><font style="font-size:11px"> </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Program </font></td></tr><tr><td><nobr>2</nobr></td><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td><nobr>3</nobr></td><td>        <font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> Main(</font><font style="color:blue">string</font><font style="font-size:11px">[] args) </font></td></tr><tr><td><nobr>4</nobr></td><td style="background-color:rgb(247, 247, 247)">        { </td></tr><tr><td><nobr>5</nobr></td><td>            var program = <font style="color:blue">new</font><font style="font-size:11px"> Program(); </font></td></tr><tr><td><nobr>6</nobr></td><td style="background-color:rgb(247, 247, 247)">            Console.WriteLine(program.SomeNumber()); </td></tr><tr><td><nobr>7</nobr></td><td>        } </td></tr><tr><td><nobr>8</nobr></td><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td><nobr>9</nobr></td><td>        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber() </font></td></tr><tr><td><nobr>10</nobr></td><td style="background-color:rgb(247, 247, 247)">        { </td></tr><tr><td><nobr>11</nobr></td><td>            <font style="color:blue">return</font><font style="font-size:11px"> 5; </font></td></tr><tr><td><nobr>12</nobr></td><td style="background-color:rgb(247, 247, 247)">        } </td></tr><tr><td><nobr>13</nobr></td><td>    } </td></tr><tr><td><nobr>14</nobr></td><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td><nobr>15</nobr></td><td>    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Extension1 </font></td></tr><tr><td><nobr>16</nobr></td><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td><nobr>17</nobr></td><td>        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber(</font><font style="color:blue">this</font><font style="font-size:11px"> Program program) </font></td></tr><tr><td><nobr>18</nobr></td><td style="background-color:rgb(247, 247, 247)">        { </td></tr><tr><td><nobr>19</nobr></td><td>            <font style="color:blue">return</font><font style="font-size:11px"> 9; </font></td></tr><tr><td><nobr>20</nobr></td><td style="background-color:rgb(247, 247, 247)">        } </td></tr><tr><td><nobr>21</nobr></td><td>    } </td></tr></tbody></table></div><br><br>In the Program#1 the output is 5.  This means that the instance field takes precedence over the extension method.<br><br><b>Program#2</b><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="border-right:1px solid gray;font-family:Courier New;font-size:11px;background-color:rgb(238, 238, 238);padding-right:5px;padding-left:10px;width:5px;color:gray;text-align:right;vertical-align:top"><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><nobr>1</nobr></td><td><font style="font-size:11px">{ </font></td></tr><tr><td><nobr>2</nobr></td><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Program </font></td></tr><tr><td><nobr>3</nobr></td><td>    { </td></tr><tr><td><nobr>4</nobr></td><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> Main(</font><font style="color:blue">string</font><font style="font-size:11px">[] args) </font></td></tr><tr><td><nobr>5</nobr></td><td>        { </td></tr><tr><td><nobr>6</nobr></td><td style="background-color:rgb(247, 247, 247)">            var program = <font style="color:blue">new</font><font style="font-size:11px"> Program(); </font></td></tr><tr><td><nobr>7</nobr></td><td>            Console.WriteLine(program.SomeNumber()); </td></tr><tr><td><nobr>8</nobr></td><td style="background-color:rgb(247, 247, 247)">        } </td></tr><tr><td><nobr>9</nobr></td><td> </td></tr><tr><td><nobr>10</nobr></td><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber() </font></td></tr><tr><td><nobr>11</nobr></td><td>        { </td></tr><tr><td><nobr>12</nobr></td><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">return</font><font style="font-size:11px"> 5; </font></td></tr><tr><td><nobr>13</nobr></td><td>        } </td></tr><tr><td><nobr>14</nobr></td><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td><nobr>15</nobr></td><td> </td></tr><tr><td><nobr>16</nobr></td><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Extension1 </font></td></tr><tr><td><nobr>17</nobr></td><td>    { </td></tr><tr><td><nobr>18</nobr></td><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber(</font><font style="color:blue">this</font><font style="font-size:11px"> Program program) </font></td></tr><tr><td><nobr>19</nobr></td><td>        { </td></tr><tr><td><nobr>20</nobr></td><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">return</font><font style="font-size:11px"> 9; </font></td></tr><tr><td><nobr>21</nobr></td><td>        } </td></tr><tr><td><nobr>22</nobr></td><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td><nobr>23</nobr></td><td> </td></tr><tr><td><nobr>24</nobr></td><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Extension2 </font></td></tr><tr><td><nobr>25</nobr></td><td>    { </td></tr><tr><td><nobr>26</nobr></td><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber(</font><font style="color:blue">this</font><font style="font-size:11px"> Program program) </font></td></tr><tr><td><nobr>27</nobr></td><td>        { </td></tr><tr><td><nobr>28</nobr></td><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">return</font><font style="font-size:11px"> 17; </font></td></tr><tr><td><nobr>29</nobr></td><td>        } </td></tr><tr><td><nobr>30</nobr></td><td style="background-color:rgb(247, 247, 247)">    } </td></tr></tbody></table></div><br>In program #2, I added a second Extension method by the same name.  This doesn't error since the compiler can still resolve this un-ambiguously to the instance method since instance methods always take precedence.<br><br><b>Program#3</b><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="border-right:1px solid gray;font-family:Courier New;font-size:11px;background-color:rgb(238, 238, 238);padding-right:5px;padding-left:10px;width:5px;color:gray;text-align:right;vertical-align:top"><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><nobr>1</nobr></td><td><font style="font-size:11px">    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Program </font></td></tr><tr><td><nobr>2</nobr></td><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td><nobr>3</nobr></td><td>        <font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> Main(</font><font style="color:blue">string</font><font style="font-size:11px">[] args) </font></td></tr><tr><td><nobr>4</nobr></td><td style="background-color:rgb(247, 247, 247)">        { </td></tr><tr><td><nobr>5</nobr></td><td>            var program = <font style="color:blue">new</font><font style="font-size:11px"> Program(); </font></td></tr><tr><td><nobr>6</nobr></td><td style="background-color:rgb(247, 247, 247)">            Console.WriteLine(program.SomeNumber()); </td></tr><tr><td><nobr>7</nobr></td><td>        } </td></tr><tr><td><nobr>8</nobr></td><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td><nobr>9</nobr></td><td> </td></tr><tr><td><nobr>10</nobr></td><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Extension1 </font></td></tr><tr><td><nobr>11</nobr></td><td>    { </td></tr><tr><td><nobr>12</nobr></td><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber(</font><font style="color:blue">this</font><font style="font-size:11px"> Program program) </font></td></tr><tr><td><nobr>13</nobr></td><td>        { </td></tr><tr><td><nobr>14</nobr></td><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">return</font><font style="font-size:11px"> 9; </font></td></tr><tr><td><nobr>15</nobr></td><td>        } </td></tr><tr><td><nobr>16</nobr></td><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td><nobr>17</nobr></td><td> </td></tr><tr><td><nobr>18</nobr></td><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Extension2 </font></td></tr><tr><td><nobr>19</nobr></td><td>    { </td></tr><tr><td><nobr>20</nobr></td><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">int</font><font style="font-size:11px"> SomeNumber(</font><font style="color:blue">this</font><font style="font-size:11px"> Program program) </font></td></tr><tr><td><nobr>21</nobr></td><td>        { </td></tr><tr><td><nobr>22</nobr></td><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">return</font><font style="font-size:11px"> 17; </font></td></tr><tr><td><nobr>23</nobr></td><td>        } </td></tr><tr><td><nobr>24</nobr></td><td style="background-color:rgb(247, 247, 247)">    } </td></tr></tbody></table></div><br>In program#3, I removed the instance method and now the compiler throws this error:<br><font color="#ff0000">Error    The call is ambiguous between the following methods or properties: '<br>Extension1.SomeNumber(Program)' and 'Extension2.SomeNumber(Program)' </font><br><br>Since extension methods have the same precedence as other extension method, having two with the same name and no instance method causes the compiler to throw a fit since it can't decide.<br><br>I hope that helps.<br><hr size="1" align="left" width="25%">-MattThu, 03 Jul 2008 17:27:07 Z2008-07-03T17:27:07Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/e42f1511-39e7-4fed-9e56-0cc19c00d33d#a9d40e03-5aed-4c2f-9419-b0d20f5e96b2http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/e42f1511-39e7-4fed-9e56-0cc19c00d33d#a9d40e03-5aed-4c2f-9419-b0d20f5e96b2Paul Houlehttp://social.msdn.microsoft.com/Profile/en-US/?user=Paul%20HouleLooking for Feedback About Extension Methods ArticleVery much so.  Thanks! Tue, 08 Jul 2008 20:02:39 Z2008-07-08T20:02:39Z