How to Add Extension Methods to an Existing Static Class

Can I add extension methods to an existing static class?

No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.

 public static class ConfigurationManagerWrapper
{
public static ConfigurationSection GetSection( string name )
{
return ConfigurationManager.GetSection( name );
}

.....

public static ConfigurationSection GetWidgetSection()
{
return GetSection( "widgets" );
}
}

Extension methods on a static class?

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.

Static extension methods

In short, no, you can't.

Long answer, extension methods are just syntactic sugar. IE:

If you have an extension method on string let's say:

public static string SomeStringExtension(this string s)
{
//whatever..
}

When you then call it:

myString.SomeStringExtension();

The compiler just turns it into:

ExtensionClass.SomeStringExtension(myString);

So as you can see, there's no way to do that for static methods.

And another thing just dawned on me: what would really be the point of being able to add static methods on existing classes? You can just have your own helper class that does the same thing, so what's really the benefit in being able to do:

Bool.Parse(..)

vs.

Helper.ParseBool(..);

Doesn't really bring much to the table...

c# extend String Class with static method

No, you can't.

For more information, and an explanation, see these earlier questions:

  • Can I add extension methods to an existing static class?
  • Static extension methods
  • Why aren't C# static class extension methods supported?

How to extend a static class in C#

You can't. You will have to create a new one.

Or you could create an existing package, like Fluent Assertions.

Can you add extension methods that you call like static methods?

According to Microsoft, "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type".

Yes, extension methods are static methods. They can all be called in the normal way as static methods, as extension instance methods on the type they "extend", and they can even be called as extension methods on a null reference.

For example:

public static class Extensions {
public static bool IsNullOrEmpty(this string theString) {
return string.IsNullOrEmpty(theString);
}
}

// Code elsewhere.
string test = null;
Console.WriteLine(test.IsNullOrEmpty()); // Valid code.
Console.WriteLine(Extensions.IsNullOrEmpty(test)); // Valid code.

Edit:

Is there a way to add an extension method that it called as if it was a static method?

Do you mean you want to call, for example, string.MyExtensionMethod()? In this case, no, there is no way to do that.

extension method to extend static class

No, extension methods can only be used to add instance methods, not static methods (or even properties). Extension methods are really just syntactic sugar around static methods. For instance, when you use an extension method such as Count():

var list = GetList();
var size = list.Count();

This is actually compiled to:

var list = GetList();
var size = Enumerable.Count(list);

You can't add additional static methods to an existing class using extension methods.

Why can't I call an extension method as a static method when using static import?

Because of language design:

Using static makes extension methods declared in the specified type
available for extension method lookup. However, the names of the
extension methods are not imported into scope for unqualified
reference in code.

using Directive

Create an extension method to override from different classes

Your current approach is OK.

Extension method is the static method too. There won't be any difference at runtime, just a syntax one:

this.myToString()

instead of

Helper.myToString(this)

Since writing extension methods for object isn't a good choice (it will be available for everything), Helper class is preferable.



Related Topics



Leave a reply



Submit