﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Nathan commented on Generic extension methods</title><description>I'm really surprised that you had not come across this already.  I've been doing this since early CTP releases.  Generic extension methods are fundamental to C# 3.0 programming.
  
  
Glad you found it.  Now I'm sure you'll come up with lots of interesting uses for this feature.
</description><link>http://ayende.com/3163/generic-extension-methods#comment18</link><guid>http://ayende.com/3163/generic-extension-methods#comment18</guid><pubDate>Wed, 27 Feb 2008 04:52:06 GMT</pubDate></item><item><title>Fernando commented on Generic extension methods</title><description>Ayende,
  
  
Maybe this is by design (callvirt vs. call). This blog post explains it: http://blogs.msdn.com/howard_dierking/archive/2007/02/09/more-c-3-0-extension-methods.aspx
  
  
Here's another one:
  
http://bradwilson.typepad.com/blog/2008/01/c-30-extension.html
  
</description><link>http://ayende.com/3163/generic-extension-methods#comment17</link><guid>http://ayende.com/3163/generic-extension-methods#comment17</guid><pubDate>Mon, 25 Feb 2008 19:23:16 GMT</pubDate></item><item><title>Luke Breuer commented on Generic extension methods</title><description>Ayende, I've been using the following code (which will be mangled by the blog comment engine, grrrr) for a while:
  
  
public static string Join(this IEnumerable&lt;string&gt; strings, string delimiter)
  
{
  
    return strings.Any()
  
        ? string.Join(delimiter, strings as string[] ?? strings.ToArray())
  
        : "";
  
}
  
  
I also echo Thomas Krause: LINQ is _built_ on IEnumerable&lt;T&gt; and extension methods working with it, among other things.
</description><link>http://ayende.com/3163/generic-extension-methods#comment16</link><guid>http://ayende.com/3163/generic-extension-methods#comment16</guid><pubDate>Mon, 25 Feb 2008 19:00:14 GMT</pubDate></item><item><title>Jimmy Bogard commented on Generic extension methods</title><description>Also, not only does it compile successfully, but your generic constraints are correctly interpreted by Intellisense.  It's not all IProcessor&lt;T&gt; implementations that catch it, it's _only_ those that conform to the constraint.
  
  
Even removing the constraint on the interface, the Intellisense still pops up correctly if you constrain the generic method.  I haven't tried R# code completion yet, this is only VS built-in intellisense.
</description><link>http://ayende.com/3163/generic-extension-methods#comment15</link><guid>http://ayende.com/3163/generic-extension-methods#comment15</guid><pubDate>Mon, 25 Feb 2008 12:36:43 GMT</pubDate></item><item><title>Jon Skeet commented on Generic extension methods</title><description>Ayende,
  
  
Even talking about generic methods, LINQ is still stuffed full of examples.
  
  
Take Select - yes, it acts on a particular generic interface, but it has to project to another generic interface (a sequence of the result type) and that result type is specified by a type parameter.
  
  
Indeed, even simple methods like Where which only act on a generic interface are still generic methods - it's not like the type itself is Enumerable&lt;T&gt; with extension methods for the corresponding IEnumerable&lt;T&gt;. There are relatively few methods in LINQ which *aren't* generic methods.
  
  
Jon
</description><link>http://ayende.com/3163/generic-extension-methods#comment14</link><guid>http://ayende.com/3163/generic-extension-methods#comment14</guid><pubDate>Mon, 25 Feb 2008 08:36:37 GMT</pubDate></item><item><title>Jimmy Bogard commented on Generic extension methods</title><description>Awww old news
  
  
http://grabbagoft.blogspot.com/2007/07/constrained-generic-extension-methods.html
  
  
It's very interesting indeed.  I've been playing around with specification-type extensions, for things like IComparable&lt;T&gt;.
  
  
It was very surprising the first time I compiled it.  We had a little bet at the office whether it would or not.  And an interesting DBC-style implementation too:
  
  
http://eltonomicon.blogspot.com/2007/10/static-class-extensibility-via.html
  
  
You do still run into the problems of generic constraints, and you have to weigh whether you target the constraint directly or use constraints.
</description><link>http://ayende.com/3163/generic-extension-methods#comment13</link><guid>http://ayende.com/3163/generic-extension-methods#comment13</guid><pubDate>Mon, 25 Feb 2008 04:37:02 GMT</pubDate></item><item><title>Ayende Rahien commented on Generic extension methods</title><description>Albert,
  
I am surrised that the compiler can pick it up, actually.
</description><link>http://ayende.com/3163/generic-extension-methods#comment12</link><guid>http://ayende.com/3163/generic-extension-methods#comment12</guid><pubDate>Mon, 25 Feb 2008 01:27:56 GMT</pubDate></item><item><title>Ayende Rahien commented on Generic extension methods</title><description>Thomas,
  
Methods for generic interface, yes.
  
Generic methods, I wouldn't have expected that.
</description><link>http://ayende.com/3163/generic-extension-methods#comment11</link><guid>http://ayende.com/3163/generic-extension-methods#comment11</guid><pubDate>Mon, 25 Feb 2008 01:27:28 GMT</pubDate></item><item><title>Julian Birch commented on Generic extension methods</title><description>Actually, the thing I find most surprising is that the type inference works.  I'm pretty sure the pretty similar code you could write for .NET 2.0 doesn't compile.
  
  
The whole "extension methods aren't instance methods" is a bit weird, although I have to admit I've already abused it.  (i.e. made it not throw an exception)
</description><link>http://ayende.com/3163/generic-extension-methods#comment10</link><guid>http://ayende.com/3163/generic-extension-methods#comment10</guid><pubDate>Mon, 25 Feb 2008 01:21:53 GMT</pubDate></item><item><title>Ayende Rahien commented on Generic extension methods</title><description>VIjay ,
  
Yes, it does.
  
It is significantly smarter than it was for 2.0
</description><link>http://ayende.com/3163/generic-extension-methods#comment9</link><guid>http://ayende.com/3163/generic-extension-methods#comment9</guid><pubDate>Mon, 25 Feb 2008 01:20:25 GMT</pubDate></item><item><title>alberto commented on Generic extension methods</title><description>Well, maybe I am missing the obvious, but I don't see anything strange. It's a static method behind the scenes, so the compiler shouldn't care whether you are passing a null argument to it. I don't see why being a generic method should change that fact.
</description><link>http://ayende.com/3163/generic-extension-methods#comment8</link><guid>http://ayende.com/3163/generic-extension-methods#comment8</guid><pubDate>Mon, 25 Feb 2008 01:08:57 GMT</pubDate></item><item><title>Thomas Krause commented on Generic extension methods</title><description>Why are you surprised?
  
  
Isn't the whole LINQ stuff based on extension methods for the (generic) IEnumerable&lt;T&gt; interface?
</description><link>http://ayende.com/3163/generic-extension-methods#comment7</link><guid>http://ayende.com/3163/generic-extension-methods#comment7</guid><pubDate>Mon, 25 Feb 2008 01:08:34 GMT</pubDate></item><item><title>VIjay Santhanam commented on Generic extension methods</title><description>It seems like one must check for a not null "this" argument as a precondition in extension methods.
  
  
Also, the compiler is smarter with generic methods. Notice you don't have to specify the parameters to any generic System.Linq ext methods. 
  
  
Two typical linq extension methods include
  
public static IQueryable AsQueryable(this IEnumerable source);
  
public static IQueryable&lt;TElement&gt; AsQueryable&lt;TElement&gt;(this IEnumerable&lt;TElement&gt; source);
  
  
  
If u have the code 
  
  
string[] items = {"a","b"."c" ,"two"};
  
items.ToQueryable(); 
  
the second line will use the generic method and not the first signature.
  
  
Is this a case of the compiler inferring type when picking call methods?
</description><link>http://ayende.com/3163/generic-extension-methods#comment6</link><guid>http://ayende.com/3163/generic-extension-methods#comment6</guid><pubDate>Mon, 25 Feb 2008 00:58:12 GMT</pubDate></item><item><title>Ayende Rahien commented on Generic extension methods</title><description>I am trying to find my previous code highlighter.
  
  
As for why it compiles, that is an interface, not a class
</description><link>http://ayende.com/3163/generic-extension-methods#comment5</link><guid>http://ayende.com/3163/generic-extension-methods#comment5</guid><pubDate>Mon, 25 Feb 2008 00:23:01 GMT</pubDate></item><item><title>Craig Shearer commented on Generic extension methods</title><description>There are two things about this that puzzle me - firstly and superficially, there is a spelling mistake - Desitnation instead of Destination - but it doesn't matter to the compiler.
  
  
But, there's no implementation of the getter (TDestination Desitnation {get;}) That is weird!
  
  
Oh, and if you're going to show us code, it would be nice if it didn't have line numbers - it makes it terrible to cut and paste.
  
  
</description><link>http://ayende.com/3163/generic-extension-methods#comment4</link><guid>http://ayende.com/3163/generic-extension-methods#comment4</guid><pubDate>Mon, 25 Feb 2008 00:16:18 GMT</pubDate></item><item><title>Andrew commented on Generic extension methods</title><description>Actually, I did look at the top afterwards, at the name of the post - and yeah... red faced now :)
  
  
Yep, we have  a couple of generic methods as extension methods too - comes nice and handy when you need to do the same thing in a few different cases.
  
  
We're starting to build up some core extension methods to be shared between specific projects, and each project has it's own extension method library - but we do like having some generics used on the global ones that can work between a couple of project idiosyncrasies.
  
  
Apologies to the assume, it did make an ass out of me, at least :)
</description><link>http://ayende.com/3163/generic-extension-methods#comment3</link><guid>http://ayende.com/3163/generic-extension-methods#comment3</guid><pubDate>Sun, 24 Feb 2008 23:09:08 GMT</pubDate></item><item><title>Ayende Rahien commented on Generic extension methods</title><description>Andrew,
  
The extension method is a generic method.
  
I was surprised that _that_ worked.
</description><link>http://ayende.com/3163/generic-extension-methods#comment2</link><guid>http://ayende.com/3163/generic-extension-methods#comment2</guid><pubDate>Sun, 24 Feb 2008 23:04:20 GMT</pubDate></item><item><title>Andrew commented on Generic extension methods</title><description>Maybe I'm missing something but any reason it shouldn't?
  
  
Because it's a nulled variable?
  
  
You can do the same with any method on a variable:
  
        static void Main(string[] args)
  
        {
  
            string p = null;
  
            string[] q = p.Split(',');
  
        }
  
  
Sure it'd fail during execution, but not on compile.
  
  
We figured this out a while back, and do a test for a null reference that throws an exception, if it's a critical part of the app.
</description><link>http://ayende.com/3163/generic-extension-methods#comment1</link><guid>http://ayende.com/3163/generic-extension-methods#comment1</guid><pubDate>Sun, 24 Feb 2008 23:01:00 GMT</pubDate></item></channel></rss>