This project is read-only.
3
Vote

Static Extension Methods

description

A "nice to have" feature that would be helpful is static extension methods. Wait, you say, aren't all extension method static? Yes, but there are a lot of cases where it would be helpful to add extension methods to a class, rather than an instance of that class. The most famous example of this being the Math class. Currently, if you want to add your own Math utilities you put them in your own static MathUtil class or similar.

Instead of having to think about which Math library you want to call, it would be much nicer to simply do:
public static class MathExtensions 
{
    //System.Math.Pow only supports doubles
    public static int Pow(static Math, int x, int y)
    {
         //Code
    }
}

public class Foo 
{
    public void Main()
    {
        int z = Math.Pow(5, 2);
    }
}

comments

AdamSpeight2008 wrote Apr 14, 2014 at 7:06 PM

You can do the follow in VB.net
Namespace Global.System
  <HideModuleName>
  Public Module MathExts

    <Runtime.CompilerServices.Extension>
    Public Function Pow(m As System.Math, x As Integer, y As Integer) As Integer
      ' Code 
    End Function
  End Module

End Namespace
And it'll appear as an overload of Math.Pow

tom103 wrote Apr 21, 2014 at 9:56 PM

I used to think it would be a good idea too, but the more I think about it, the more I realize it would probably not be such a good thing

tom103 wrote Apr 21, 2014 at 9:58 PM

Sorry about the truncated message above; I wanted to discard it, not post it... apparently Ctrl+F4 submits the message instead of closing the tab (WTF?)