Interviews - C# Corner

Prasad Raveendran
Create an extension method using a given third-party DLL. Say, I get a dll from a third party and it has only one method. Now, I want to add a new method to it but I can't get the dll's source code. How?
By Prasad Raveendran in C# on Apr 26 2019
  • Ramana Kumar
    Jun, 2019 8

    Extension methods are additional custom methods which were originally not included with the class. It is a mechanism of adding methods into an existing class or structure also without modifying the source code of original type. Below is an example adding a method to the int class:namespace ExtensionMethods {public static class IntExtensions{public static bool IsGreaterThan(this int i, int value){return i > value;}} }

    • 2
  • Prasad Raveendran
    Apr, 2019 26

    You can always add an Extension Method to a class that you have no control over, whether it was a core .NET class or a referenced dll. Below is an example adding a method to the String class:namespace ExtensionMethods {public static class MyExtensions{public static int WordCount(this String str){return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;}} }Note: Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

    • 2

Most Popular Companies

Most Popular Job Functions