Extension methods can only be used with instance of a class. Compiler just searches for all public static methods inside all public static classes where type is preceded with keyword this
and 'attaches' them to the type making it possible to call them as instance methods but it just a convenience and in fact this methods are just the same as any other static methods are.
So the word this
cannot be used with static
class because it points to instance of some class and there is no such a thing as static instance
.
public static class Helper
{
public static bool MyExtMethod(this MyClass instance)
{
...
}
}
Compiler finds this method and attaches it to MyClass, doing something like this:
class MyClass
{
...
public bool MyExtMethod()
{
return Helper.MyExtMethod(this);
}
}
So it basically allow us to call static method on the MyClass
rather then do it directly on Helper
.
String.Empty is a read-only field not a method.
What can you do however is to create your own class that works with string and add your methods there:
class MyString
{
private readonly string value;
public string Value { get { return value; } }
public MyString(string s) { this.value = s; }
public static implicit operator MyString(string s)
{
MyString ms = new MyString(s);
return ms;
}
public static implicit operator string(MyString s)
{
return s.Value;
}
public static String ExtensionMethod1(String input){
return "Something1";
}
public static String ExtensionMethod2(String input){
return "Something1";
}
}
Though it's not quite what you wanted