Sunday, February 3, 2019

Extension methods

Extension methods:

- Introduced in C# 3.0
- It is a mechanism of adding new methods into an existing class or structure without modifying the source code of the original type. This process does not require any permissions from the the original type and the original type does not require any recompilation.

-Extension methods are defined as static but once they are bound with any class or structure, they turn into non-static. That is why we are able to call them via instance of the class.

- if an extension method is defined as same name in the method in existing class, the extension method will not be called(the original method will be called.)
-an extension method should have one and only binding parameter, and it should be first one in the parameter list (the 'this' keyword)

-Extension methods also work on sealed class. (e.g. String is a sealed class). Still you can write extension method for sealed class.

-- What we achieve by extension methods can also be done by Inheritance. But you can not apply inheritance on sealed class. Also, Inheritance does not work on structures. Extension methods also work on sealed class.

No comments:

Post a Comment

Asp.net Core Continued

65 66 67 68 69 70 71 65 ASP.NET Core Identity It is a membership system Step 1 : Inherit from IdentityDbContext class instead of ...