Hello C# beginners. In this blog, we will see what an extension method via a concrete example.
Why use extension Methods?
If we need to add a method called ExtensionMethod to Int class: we should create a derivative class and add our methods to it, correct?
The response is NO: Because int class is sealed so we can’t inherit from it but .NET framework contains a powerful solution which is “Extension Methods.”
How could we do that?
To add an extension Method of int type, add a static method, the first attribute type must be (this int)
This is used to specify that int is our extended Type.
- public static type ExtensionMethod (this int param, params...)
- {
- // instructions
- }
To use this extension:
- int i;
- i.ExtensionMethod(params ..)
Advantages
No Need to create a derivate class.
No need to change original class.
No need to recompile.
Calling of an extension method is simple.
For more details, you can read the links below.
- https://msdn.microsoft.com/en-us/library/bb383977.aspx
- https://msdn.microsoft.com/en-us/library/bb311042.aspx

muhammed yasirPosted Jul 12, 2019, 3:00 AM
Hai iam yasir.i want binding text in label using mvvm method .canot update label text please help me
Viknaraj ManogararajahPosted Jul 21, 2018, 10:59 PM
Nice Article, Thank you for sharing
Ishrak El AndaloussiPosted May 21, 2018, 4:25 AM
Thank you ! for your constructive comments that are useful and show that the community contains brilliant members like you.
Rebai HamidaPosted May 18, 2018, 7:08 AM
Hello, I will add that it's an empty and poor content. (You can add this comment to my article https://www.c-sharpcorner.com/article/mobile-alerts-in-xamarim-forms as you did before) comments are important to go ahead and improve our publications, if you are not able to accept them and you take that personal (you did the same in a previous blog), I advise you to stop adding anything, I said that when we explain a technique we add an example else definitions exists anywhere!
Rebai HamidaPosted May 17, 2018, 1:25 PM
Hello, if we post a blog, we use an example to explain the technique or the theory, I can't see the added value if we give only definition and the advantages (that already exist in MSDN as metionned) this article that have the same context is more useful and rich that this one: https://www.c-sharpcorner.com/blogs/learn-about-extension-methods-in-c-sharp2