C# › Extension Methods
What is an extension method, and what syntax makes a static method into one?
BeginnerAnswer
An extension method lets you 'add' new methods to an existing type (even one you don't own the source for, like a built-in .NET type) without modifying it — declared as a static method in a static class, with the FIRST parameter prefixed with 'this', specifying which type it extends.
public static class StringExtensions {
public static bool IsNullOrEmpty(this string? value) => string.IsNullOrEmpty(value);
}
string? name = null;
bool empty = name.IsNullOrEmpty(); // called as if it were an instance methodReal-world example Adding a convenient .IsValidEmail() check directly onto the built-in 'string' type without subclassing or wrapping it.