filmov
tv
C# Beginner to advanced - Lesson 49 - Extension Methods | How to create Extension methods and use it
Показать описание
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.
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.
Комментарии