Extension methods on a static class?

user34537 picture user34537 · Jan 5, 2010 · Viewed 10.2k times · Source

I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc()

static public class SomeName
{
    static public int HelperFunction(this SomeClass v)

Answer

this. __curious_geek picture this. __curious_geek · Jan 5, 2010

You can't have extension methods on static classes because extension methods are only applicable to instantiable types and static classes cannot be instantiated.

Check this code..

    public static bool IsEmail(this string email)
    {
        if (email != null)
        {
            return Regex.IsMatch(email, "EmailPattern");
        }

        return false;
    }

First parameter to IsEmail() is the extending type instance and not just the type itself. You can never have an instance of a static type.