Posted by Simplify DotNet on December 28, 2020 in C#, extension class, extension method | No comments
Extension Methods
Extension method in C# enable us to extend the functionality of a type without deriving from it, in simple terms we can add a method to an existing type. Extension method is a static method in a static class. Extension method will be very clear when you will see below example.
So as you can see in the below example we have created a static class ExtensionDemo and in that we have a static method ToUpperLower which modifies the string passed to method and then returns the method. In the ToUpperLower method we are changing alternate character in the string to upper case. Now as you can see, we can call the ToUpperLower method on a string type of variable as the first parameter accepted by extension method which needs to be accepted using this keyword is of string type.
All the extension methods are denoted by a downward arrow symbol as show below.
- Extension method feature is available from C# 3.0.
- Extension method must be a static method in a static class.
- First parameter of an extension method must start with this keyword of a given type in .NET.
So today we have looked in to the extension methods, and how we can use them in C#.
Thank You...…
For any issues related to C#, .NET, or jQuery please or to provide any feedback please write to me on prson94@gmail.com and I will try my best to get back to you as soon as possible.
Email Id - prson94@gmail.com
0 comments:
Post a Comment