Calling extension methods in the other assemblies

We have successfully created the IsPalindrome() extension method in the previous section. It's quite easy to call the extension method since it's defined inside the same namespace as the caller method. In other words, the IsPalindrome() extension method and the Main() method are in the same namespace. We don't need to add a reference to any module since the method is there along with the caller. However, in common practice, we can create extension methods in the other assemblies, which we usually call class library. The use of the library will ease the use of the extension method since it can be reused, so we can use the extension method in many projects.

Referencing a namespace

We are going to create an extension method in the Class Library and call it in another project. Let's create a new Class Library project named ReferencingNamespaceLib.csproj and insert the following code into the ExtensionMethodsClass.cs file:

using System; 
namespaceReferencingNamespaceLib 
{ 
  public static class ExtensionMethodsClass 
  { 
    public static byte[] ConvertToHex(this string str) 
    { 
      int i = 0; 
      byte[] HexArray = new byte[str.Length]; 
      foreach (char ch in str) 
      { 
        HexArray[i++] = Convert.ToByte(ch); 
      } 
      returnHexArray; 
    } 
  } 
} 

From the preceding code, we can see that we create the ConvertToHex() extension method inside the ExtensionMethodsClass class in the ReferencingNamespaceLib namespace. The use of the ConvertToHex() extension method is to convert each character in the string to ASCII code and store it in the byte array. Now let's take a look at the following code, which will call the extension method, which we can find in the ReferencingNamespace.csproj project:

using System; 
using ReferencingNamespaceLib; 
namespace ReferencingNamespace 
{ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      int i = 0; 
      string strData = "Functional in C#"; 
      byte[] byteData = strData.ConvertToHex(); 
      foreach (char c in strData) 
      { 
        Console.WriteLine("{0} = 0x{1:X2} ({2})", 
        c.ToString(), 
        byteData[i], 
        byteData[i++]); 
      } 
    } 
  } 
} 

From the preceding code, we can see that we call the ConvertToHex() extension method from the instance of string, which is strData, as follows:

string strData = "Functional in C#"; 
byte[] byteData = strData.ConvertToHex(); 

However, in order to invoke the ConvertToHex() method from the string instance, we have to refer to the ReferencingNamespaceLib assembly and also import the namespace of the reference assembly. To import the assembly, we have to use using along with ReferencingNamespaceLib as shown in the following code snippet:

usingReferencingNamespaceLib; 

If we run the ReferencingNamespace.csproj project, we will get the following output on the console:

Referencing a namespace

As we can see, each character in C# sentences is converted into ASCII code invoked the extension method we created for the string type by referencing a namespace in both hexadecimal and decimal formats. This also proves that we have successfully in another assembly.

Piggybacking a namespace

We can, if we want, piggyback on the namespace where the string type lives, which is the System namespace, so that we don't need to import a custom namespace to use the extension method. Piggybacking a namespace is also good for our standard programming approach. Let's refactor our previous ReferencingNamespaceLib.csproj code using the following code, which we can find in the PiggybackingNamespaceLib.csproj project:

namespace System 
{ 
  public static class ExtensionMethodsClass 
  { 
    public static byte[] ConvertToHex(this string str) 
    { 
      int i = 0; 
      byte[] HexArray = new byte[str.Length]; 
      foreach (char ch in str) 
      { 
        HexArray[i++] = Convert.ToByte(ch); 
      } 
      return HexArray; 
    } 
  } 
} 

If we observe the class name, the ConvertToHex() method signature, or the implementation of the method, we will find that there's no difference between the ReferencingNamespaceLib.csproj and PiggybackingNamespaceLib.csproj projects. However, if we look at the namespace name, we will find that now it's System instead of PiggybackingNamespaceLib. The reason we use the System namespace is to create an extension method in the selected namespace. Since we want to extend the ability of the string type that lives in the System namespace, we have to extend the System namespace as well. We do not need to import the System namespace using a using keyword since the ConvertToHex() method lives in the System namespace. Now, let's take a look at the following code in order to invoke the ConvertToHex() method inside the System namespace, which we can find in the PiggybackingNamespace.csproj project:

using System; 
namespace PiggybackingNamespace 
{ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      int i = 0; 
      string strData = "Piggybacking"; 
      byte[] byteData = strData.ConvertToHex(); 
      foreach (char c in strData) 
      { 
        Console.WriteLine("{0} = 0x{1:X2} ({2})", 
        c.ToString(), 
        byteData[i], 
        byteData[i++]); 
      } 
    } 
  } 
} 

We refactor the preceding code from the ReferencingNamespace.csproj project, and again, we don't find any differences between the PiggybackingNamespace.csproj and ReferencingNamespace.csproj projects except that there is no import to the custom namespace in the PiggybackingNamespace.csproj project, which the ReferencingNamespace.csproj project has:

using ReferencingNamespaceLib; 

We don't need to import the custom namespace since we create the extension method in the System namespace. However, we still need to refer to the assembly where the extension method is defined. We can expect an output like what is shown in the following screenshot:

Piggybacking a namespace

We have successfully invoked the ConvertToHex() extension method and found it useful for getting the ASCII code from the string data type.

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

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