In C#, extension methods allow you to "add" methods to existing types without modifying the original type or using inheritance. These methods are defined in a static class , and the method itself must also be static , with the first parameter preceded by the this keyword to indicate the type it's extending. 🔹 Syntax of Extension Method csharp public static class ExtensionClass { public static returnType MethodName ( this TypeToExtend obj, other parameters) { // method body } } 🔹 Example 1: Extension Method on string Type csharp // Extension class public static class StringExtensions { public static int WordCount ( this string str) { if ( string .IsNullOrWhiteSpace(str)) return 0 ; return str.Split( ' ' ).Length; } } // Usage class Program { static void Main () { string msg = "Hello from TechForest Software" ; int count = msg.WordCount(); // ...
Hi, I'm Shiva Gautam. With over 15 years of diverse experience in various IT domains, I am now an entrepreneur focusing on both training and software development. My expertise spans across multiple technologies, and I've authored numerous articles on subjects including Java, Python, REACT, NODE, ANGULAR, Cloud, software testing, Django framework, C#, Flutter, Salesforce, Ruby on Rails, .NET, and more. For additional information, please visit shivaconceptsolution.com and kangaroosoftware.net.