C# Beginner to advanced - Lesson 49 - Extension Methods | How to create Extension methods and use it

preview_player
Показать описание
An Extension Method is:

1. It is a static method.
2. It must be located in a static class.
3. It uses the "this" keyword as the first parameter with a type in .Net and this method will called by a given type instance on the client side.
4. It also shown by VS intellisense. When we press the dot (.) after a type instance then it comes in VS intellisense.
5. An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement.
6. You can give any name of for the class that has an Extension Method but the class should be static.
7. If you want to add new methods to a type and you don't have the source code for it then the solution is to use and implement Extension Methods of that type.
8. If you create Extension Methods that have the same signature methods as the type you are extending then the Extension Methods will never be called.

Advantages:
1. The main advantage of the extension method is to add new methods in the existing class without using inheritance.
2. You can add new methods in the existing class without modifying the source code of the existing class.
3. It can also work with sealed class.
Рекомендации по теме
Комментарии
Автор

Thank you for watching this video. Cheers! would you mind hitting *_like_* button and *_subscribe_* to our channel! That will be awesome. We would be grateful to you. And also share this video in your circle via whatsapp, facebook and twitter.

AnkproTraining
Автор

Sir, why did you use the static class Extension??
I mean you haven't utilized the "Extension" classname in the main method.
In line number 26, the "WordCount" method was invoked using
"userinput. Wordcount()".
So what is the need for using the static class "Extension"??

lennonfernandes