Back to course

Extension Methods

C# Zero to Hero: Comprehensive Programming Masterclass

Extension Methods

Extension methods allow you to "add" methods to existing types without creating a new derived type.

Syntax

Must be defined in a static class and the first parameter uses this.

csharp public static class StringExtensions { public static bool IsNumeric(this string str) { return double.TryParse(str, out _); } }

// Usage string myValue = "123"; bool check = myValue.IsNumeric();