Extension Methods In C#

In C#,  Extension methods can be identified by this logo. Extension methods can be defined in C# in a separate .cs file.

The extension method is called an instance method but is actually a static method. The instance pointer "this" is a parameter, and you must specify the "this" keyword before the appropriate parameter you want the method to be called upon.

The difference between a regular static method and an extension method is that, in a static method, we first provide the class name and then we call the method. In the extension method, the "this" keyword is in the parameter list. You can directly call this by a variable.

Create an Extension method.

  1. Open Visual Studio
  2. "File" ">>" New >> "Web Site..."
  3. Select "ASP.NET Empty Web Site", then click "OK"
  4. Add a new WebForm
  5. Add a new Class

ExtentionMethods.cs

Ebook Download
View all
Learn
View all