What are extension methods?Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.How to use extension methods?An extension method is a static method of a static class, where the this modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.Program to show how to use extension methodsCreate a project Class Library as:using System;using System.Text;namespace ClassLibExtMethod{ public class Class1 { public string Display() { return ("I m in Display"); } public string Print() { return ("I m in Print"); } }}Now create a new project File -> New -> ProjectAdd the reference of the previously created class library in this project.Code as following and use the ClassLibExtMEthod.dll in your namespace:using System;using System.Text;using ClassLibExtMethod;namespace ExtensionMethod1{ public static class XX { public static void NewMethod(this Class1 ob) { Console.WriteLine("Hello I m extended method"); } } class Program { static void Main(string[] args) { Class1 ob = new Class1(); ob.Display(); ob.Print(); ob.NewMethod(); Console.ReadKey(); } }}You can see the extension method with an arrow (different from normal method sign), as they were instance methods on the extended type. See the figure below:Output of the above programBenefits of extension methods
Arrays are Structurally equatable in .NET 4.0
Working with Controls using C# in windows programming
Great article. It is very useful. Thank you for posting this article.
Very clear and explained each step with relevant points, well organized steps...well done
Short and Sweet