Limitations of the extension method

Although the extension method is a powerful tool to achieve our functional programming, this technique still has some limitations. Here, we elaborate on the limitations the extension methods face so that we can avoid their use.

Extending a static class

As we discuss extension methods further, we know that an extension method is a static method that has public accessibility inside the static class that has public accessibility as well. The extension method will appear in the type or class we target. However, not all classes can be extended using the extension method. The existing static class will not be able to be extended. For example, the Math class has been provided by .NET. Even though the class has provided a mathematical functionality we usually use, it might be that, sometimes, we need to add another functionality to the Math class.

However, since the Math class is a static class, it's nearly impossible to extend this class by adding a single method to it. Suppose we want to add the Square() method to find the result of multiplying a number by itself. Here is the code, which we can find in the ExtendingStaticClass.csproj project, if we try to add the extension method to the Math class:

public static class StaticClassExtensionMethod 
{ 
  public static int Square(this Math m, inti) 
  { 
    return i * i; 
  } 
} 

When we compile the preceding code, there will be an error similar to what is shown in the following screenshot:

Extending a static class

The error message says that the Math static method cannot be used as a parameter of the Square() extension method. What we can do to overcome this limitation is now extend the types instead of the Math class. We can extend the int types by adding the Square() method instead of extending the Math class. Here's the code to extend the int class:

public static class StaticClassExtensionMethod 
{ 
  public static int Square(this inti) 
  { 
    return i * i; 
  } 
} 

As we can see, we extend the int types so that if we want to invoke the Square() method, we can invoke it using the following code:

public class Program 
{ 
  static void Main(string[] args) 
  { 
    int i = 60; 
    Console.WriteLine(i.Square()); 
  } 
} 

However, using this technique, we also need to extend the other types, such as float and double, to accommodate the Square() functionality in various data types.

Modifying the method implementation in the existing class or type

Although the extension method can be applied to the existing classes and types, we cannot modify the implementation of the existing method the class or type has. We can try it using the following code, which we can find in the ModifyingExistingMethod.csproj project:

namespace ModifyingExistingMethod 
{ 
  public static class ExtensionMethods 
  { 
    public static string ToString(this string str) 
    { 
      return "ToString() extension method"; 
    } 
  } 
} 

In the preceding code, we try to replace the existing ToString() method, which the string type has, with a ToString() extension method such as the one in the preceding code. Fortunately, the code will be able to be compiled successfully. Now, let's add the following code to the Main() method in the project:

namespace ModifyingExistingMethod 
{ 
  public class Program 
  { 
    static void Main(string[] args) 
    { 
      stringstr = "This is string"; 
      Console.WriteLine(str.ToString()); 
    } 
  } 
} 

However, if we run the project, the ToString() extension method will never be executed. We will get the output from the existing ToString() method instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset