News for hackers. Stuff that matters.
Re:C# Extension Methods Demystified by MichaelGG on Thursday March 28, 2013 @03:50PM (#5457202)
The C# (and presumably VB) compiler just looks for an attribute with the right namespace and name (System.Runtime.CompilerServices.ExtensionAttribute). If you're using .NET 2.0 you can get extension methods just by declaring you own attribute with that name and using it.

This is also another area where C# took a narrow view of language features and implemented the bare minimum needed to get LINQ working. Why not take a general view of type extensions, and support type extensions as a proper feature versus a hack for LINQ? That is, support all members, including static members, for type extensions.

Re:C# Extension Methods Demystified by kvb on Thursday March 28, 2013 @04:17PM (#5457340)
I also prefer F#'s more general approach, which allows all kinds of extension members (static or instance, methods, properties, and events). But members of the C# team have repeatedly explained that they were the "long pole" during the LINQ work, so I think it's perfectly understandable that they took a more limited approach.
Re:C# Extension Methods Demystified by MichaelGG on Thursday March 28, 2013 @08:09PM (#5458478)
That explanation works for C# 3, but there have been two major releases since, which added relatively little to the language. They could have rounded out the LINQ-only stuff by now.
Re:C# Extension Methods Demystified by com2kid on Thursday March 28, 2013 @08:48PM (#5458615)
C# 4.0 brought support for Dynamic, a pretty damn big change. And of course optional and default parameters, another huge boon.

Then C# 5.0 brought Async, a huge feature.

In terms of making the language more powerful I would call those on par with extending out extension methods.

Re:C# Extension Methods Demystified by magic_haze on Thursday March 28, 2013 @09:59PM (#5458851)
And I think most of the compiler team has been busy with roslyn, which sounds like a pretty big rewrite.
Re:C# Extension Methods Demystified by danabramov on Thursday March 28, 2013 @07:55PM (#5458423)
I use extension methods in a MonoTouch app for different purposes.

* adding useful hacks to Apple classes:

    UIView FindTitleView (this UINavigationBar bar)
    string GetDeviceName (this UIDevice device)
* keeping formatting consistent across the app:

    string FormatViewCount (this Document doc)
    string FormatCommentDate (this Comment comment)
* sometimes I really want to separate the class and some functions operating on its data, or providing useful helpers on top of this class:

    string SuggestTitle (this Document doc)
    Uri [] GetResources (this Document doc)
Because they live in different namespaces, I only see UIKit and formatting extensions in UI code, data analysis in business layer, and so on.
Re:C# Extension Methods Demystified by zacharypinter on Friday March 29, 2013 @12:13AM (#5459211)
One of my favorite uses of extension methods is to have semi-intelligent type coercers/converters.

For example:

  public static string AsString(this object me, string defaultValue = null)                                     
  public static int AsInt(this object me, int defaultValue = default(int))                                      
  public static float AsFloat(this object me, float defaultValue = default(float))                              
  public static double AsDouble(this object me, double defaultValue = default(double))                          
  public static Uri AsUri(this object me, Uri defaultValue = null)                                              
  public static bool AsBool(this object me, bool defaultValue = default(bool))
It's handy because you can do null checking in the extension method. Additionally, you can add some extra logic for converting specific runtime types. For example, calling .AsBool() on a string can check if the contents of the string is "1" or "true".
Re:C# Extension Methods Demystified by sageikosa on Thursday March 28, 2013 @04:44PM (#5457473)
One good use I have found for extension methods in C# is providing object.verb methods for enums.
Re:C# Extension Methods Demystified by smurph on Thursday March 28, 2013 @05:03PM (#5457591)
I tend to write object.parse(byte[] buffer) methods a lot, especially when the object type itself is being defined in another team's code.
Re:C# Extension Methods Demystified by seandhi on Thursday March 28, 2013 @04:50PM (#5457508)
I tend to use them to extend IQueryable to provide "scopes" that I am familiar with from Rails.
Re:C# Extension Methods Demystified by klausjensen on Thursday March 28, 2013 @05:12PM (#5457671)
Could you elaborate on that? An example?
Re:C# Extension Methods Demystified by fryguy on Thursday March 28, 2013 @06:44PM (#5458166)
I'm not OP, but something like:

        public static int ToInt(this BaudRate baudRate)
        {
            switch (baudRate)
            {
                case BaudRate.BaudUnsupported:
                    return 0;
                case BaudRate.Baud1200:
                    return 1200;
                case BaudRate.Baud2400:
                    return 2400;
                case BaudRate.Baud4800:
                    return 4800;
                case BaudRate.Baud9600:
                    return 9600;
                case BaudRate.Baud19200:
                    return 19200;
                case BaudRate.Baud38400:
                    return 38400;
                case BaudRate.Baud57600:
                    return 57600;
                case BaudRate.Baud115200:
                    return 115200;
                case BaudRate.Baud230400:
                    return 230400;
                default:
                    throw new ArgumentOutOfRangeException("baudRate");
            }
Not exactly the best example (and not necessarily the best way to do that), but it's a quick one.
Re:C# Extension Methods Demystified by sageikosa on Friday March 29, 2013 @06:46AM (#5460076)

    public enum AnchorFace
    {
        /// <summary>0</summary>
        XLow,
        /// <summary>1</summary>
        XHigh,
        /// <summary>2</summary>
        YLow,
        /// <summary>3</summary>
        YHigh,
        /// <summary>4</summary>
        ZLow,
        /// <summary>5</summary>
        ZHigh
    }

    public static class AnchorFaceHelper
    {
        /// <summary>Normal Vector for the face</summary>
        public static Vector3D GetNormalVector(this AnchorFace self)
        {
            switch (self)
            {
                case AnchorFace.ZLow:
                    return new Vector3D(0, 0, -1);

                case AnchorFace.ZHigh:
                    return new Vector3D(0, 0, 1);

                case AnchorFace.YLow:
                    return new Vector3D(0, -1, 0);

                case AnchorFace.YHigh:
                    return new Vector3D(0, 1, 0);

                case AnchorFace.XLow:
                    return new Vector3D(-1, 0, 0);

                default: // XHigh
                    return new Vector3D(1, 0, 0);
            }
        }
    }
Re:C# Extension Methods Demystified by tiedemann on Thursday March 28, 2013 @08:47PM (#5458607)
I'm sceptic to devs who don't treasure their own string, IEnumerable etc extensions ;)
Re:C# Extension Methods Demystified by tiedemann on Thursday March 28, 2013 @08:54PM (#5458642)
My favourite is probably String.NotNullOrEmpty, I really hate to prepend ! to stuff.
Re:C# Extension Methods Demystified by noblethrasher on Friday March 29, 2013 @02:00AM (#5459477)