<?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>More C# 3.0 – Extension Methods</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx</link><description>A while back I wrote a post about lambda expressions in C# 3.0 and how they are one of the enabling technologies behind Linq . I would like to continue that discussion today with a quick overview of something called extension methods. First, I want to</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Interesting blog posts - 2</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx#1654749</link><pubDate>Mon, 12 Feb 2007 01:08:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1654749</guid><dc:creator>.Net Adventures </dc:creator><description>&lt;p&gt;Ohad's Weblog - IE7Pro - a must have add-in if you use IE7 ! Simple Living Simple Thinking - Zip and&lt;/p&gt;
</description></item><item><title>re: More C# 3.0 – Extension Methods</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx#8398903</link><pubDate>Wed, 16 Apr 2008 14:21:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8398903</guid><dc:creator>Milan</dc:creator><description>&lt;p&gt;Hi Howard,&lt;/p&gt;
&lt;p&gt;Nice intro and connection of LINQ and extensions. I have one problem with extensions. I couldn't find a way to make an extension method that would change an instance value. i.e.&lt;/p&gt;
&lt;p&gt;public static void AddThis(this int source, int val)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; source += val;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This method doesn't throw an exception when it is called, but it doesn't change a value either. Do you have a solution for this?&lt;/p&gt;</description></item><item><title>re: More C# 3.0 – Extension Methods</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx#8399080</link><pubDate>Wed, 16 Apr 2008 18:05:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8399080</guid><dc:creator>hdierking</dc:creator><description>&lt;p&gt;Hi Milan - extension methods don't allow ref or out parameters. &amp;nbsp;Therefore, you can't do what you're trying to do for value types like int. &amp;nbsp;You can of course, change the state of reference types. &amp;nbsp;As an example, in the following 2 tests, the first test (which is the same as your code) will fail because i cannot be passed as a ref param. &amp;nbsp;The second test will pass because you are simply changing the state of a reference type.&lt;/p&gt;
&lt;p&gt;[Test]&lt;/p&gt;
&lt;p&gt;public void testExtensionMethod_ValueType() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;var i = 5;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;i.AddMe(3);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Assert.AreEqual(8, i);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[Test]&lt;/p&gt;
&lt;p&gt;public void testExtensionMethod_RefType() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;var howard = new Person {Name = &amp;quot;Howard&amp;quot;};&lt;/p&gt;
&lt;p&gt; &amp;nbsp;howard.ChangeName(&amp;quot;Howard Modified&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Assert.AreEqual(&amp;quot;Howard Modified&amp;quot;, howard.Name);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: More C# 3.0 – Extension Methods</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx#8402219</link><pubDate>Thu, 17 Apr 2008 13:34:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8402219</guid><dc:creator>Milan</dc:creator><description>&lt;p&gt;Hi Howard.&lt;/p&gt;
&lt;p&gt;I thought so, but was hoping that's not true :(&lt;/p&gt;</description></item><item><title>re: More C# 3.0 – Extension Methods</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx#8731479</link><pubDate>Mon, 14 Jul 2008 16:39:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8731479</guid><dc:creator>Abel Braaksma</dc:creator><description>&lt;p&gt;Hi Howard,&lt;/p&gt;
&lt;p&gt;Thanks for the insights in this article. What I'd hoped to see, however, is a (small) explanation of the obviousness of the extension methods being static and why it isn't possible to create an extension instance method (i.e. Int32.MyExtension()).&lt;/p&gt;
&lt;p&gt;I'd understand that the method already being static means that it cannot be &amp;quot;more&amp;quot; static (&amp;#252;ber-static???). Or do I misunderstand something and is it perfectly possible to create instance method extensions?&lt;/p&gt;
&lt;p&gt;Thanks again,&lt;/p&gt;
&lt;p&gt;Abel&lt;/p&gt;</description></item><item><title>re: More C# 3.0 – Extension Methods</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx#8736737</link><pubDate>Wed, 16 Jul 2008 06:52:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8736737</guid><dc:creator>hdierking</dc:creator><description>&lt;p&gt;Hi Abel - so in terms of the obviousness of extension methods being static, it's related to the next part of your question.&lt;/p&gt;
&lt;p&gt;Extension methods are static because for many types that you would want to create them for, you don't have access to the source code that would enable you to create instance methods (e.g. - Int32).&lt;/p&gt;
&lt;p&gt;That said, from a syntactic perspective, extension methods behave in the same manner as instance methods - enabling you to to call an Int32 extension method using the syntax you specified Int32::MyExtension&lt;/p&gt;
&lt;p&gt;essentially, the 'this' argument modifier enables a an extension method (which is static) to behave like an instance method in the code that uses it.&lt;/p&gt;
&lt;p&gt;So the short answer to your question is this - an extension method is absolutely a static method from the compiler's perspective. &amp;nbsp;However, the extension method syntax enables it to be used as if it were an instance method on the type specified for the 'this' argument.&lt;/p&gt;
&lt;p&gt;hth&lt;/p&gt;
</description></item></channel></rss>