Extension Methods in C#.Net 3.0 or more

Umar Topia.Net Full Stack Developer
CERTIFIED EXPERT
Published:
Extension methods enable us to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. They are a special kind of static method, but they are called as if they were instance methods on the extended type. For code written in C#, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Extension methods must be declared in Static Classes to make them work. To declare an extension method, we need to specify the keyword "this" as the first parameter of the method.

To Summarize the Extension Method Points:

Extension methods have the keyword this before the first argument. Static methods do not have the this keyword in its argument declaration.
When extension methods are consumed, the argument that was declared with the keyword this is not passed. When static methods are consumed, no arguments are skipped. All expected arguments must be entered.
Extension methods can be defined only in a static class. For static methods, this is not a requirement. Static methods can exist in a regular class as well as in a static class.
Extension methods can be called only on instances values.
It is important to note that extension methods can't access the private methods in the extended type.
If we want to add new methods to a type, we don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
If we create extension methods that have the same signature methods inside the type we are extending, then the extension methods will never be called.
Extension methods cannot be used to override existing methods.
The concept of extension methods cannot be applied to fields, properties or events.
We need to use extension methods sparingly....overuse can be a bad thing!
Extension methods, though static in nature, can be called only on instances. Trying to call them on a class will result in compilation errors. The class instances on which they are called are determined by the first argument in the declaration, the one having the keyword this.

Example:-

namespace TestExtensionMethods
                      {
                          public static class MyExtensionMethods
                          {
                              public static Int32 GetInt32Value(this string iValue)
                              {
                                  Int32 iReturnValue = Convert.ToInt32(iValue);
                                  return iReturnValue;
                              }
                          }
                      }
                      
                      namespace TestExtensionMethods
                      {
                          public class TestClass
                          {
                              public TestClass()
                              {
                              }
                      
                              private void CheckExtensionValue()
                              {
                                  string sValue = "12";
                                  int iNewValue = sValue.GetInt32Value();
                              }
                          }
                      }
                      

Open in new window

0
3,032 Views
Umar Topia.Net Full Stack Developer
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.