Skip to content

C# Feature – Extension methods

Extension methods add static methods callable as if they were instance methods on the target type.

C# 3 (2007)

var text = ToTitleCase("hello world");
var text = "hello world".ToTitleCase();
public static class StringExtensions
{
public static string ToTitleCase(this string s) =>
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s);
}
  • Don’t overuse; avoid polluting IntelliSense. Prefer domain-relevant extensions.
  • Works across assemblies with using.