Arnold Zahrneinder
Arnold Zahrneinder

Reputation: 5200

Writing extension method for String type as ( String.Extension NOT "String".Extension)

Writing an extension for String is easy but the problem is that is always appears as "MyString".ExtensionMethod() if written like:

public static class Extensions{
   public static String ExtensionMethod(this String input){
      return "Something";
   }

Now what if we need to write an extension method for the String type that can be used like:

 var value = String.MyExtensionMethod();

Similar to the String.Empty.

What is the right way to define to the compiler that we want it that way?

Upvotes: 1

Views: 75

Answers (4)

Fabjan
Fabjan

Reputation: 13676

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

Upvotes: 0

Michael Mairegger
Michael Mairegger

Reputation: 7301

This is impossible to have static extension methods. Furthermore you cannot add static members to any class you do not own, also the use of partial is only possible within the same assemlby. By owning I mean that you have the source and do not use a dll.

In my case I have a StringExtensions class containing the custom static methods.

Upvotes: 0

nvoigt
nvoigt

Reputation: 77374

String.Empty is a public static field.

You cannot create something like that as an extension method.

Extension methods only support instance-level public methods.

Upvotes: 3

nozzleman
nozzleman

Reputation: 9669

This would require the possibility of adding static Extension Methods which is not possilbe.

There are several answers in SO which cover the topic, and how to overcome it.

See Can I add extension methods to an existing static class? for example.

Upvotes: 1

Related Questions