C# Feature – Extension methods
Overview
Section titled “Overview”Extension methods add static methods callable as if they were instance methods on the target type.
Introduced In
Section titled “Introduced In”C# 3 (2007)
Before
Section titled “Before”var text = ToTitleCase("hello world");
var text = "hello world".ToTitleCase();
Annotated Example
Section titled “Annotated Example”public static class StringExtensions{ public static string ToTitleCase(this string s) => System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s);}
Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Don’t overuse; avoid polluting IntelliSense. Prefer domain-relevant extensions.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- Works across assemblies with
using
.