How it works
An extension method lets a static function be called with instance-style syntax. NormalizeId receives a string through its this parameter, trims surrounding whitespace, and lowercases using invariant casing. The two visibly different inputs therefore produce the same normalized identifier in this small example.
The method does not add mutable state to string, override string's implementation, or edit the original characters in place. Strings are immutable, and the function returns a result for the caller to use. Writing an extension with familiar dot syntax does not change the underlying ownership or copying rules of the receiver type.
Normalization is a domain policy. Trimming and lowercasing may be appropriate for one internal identifier but wrong for a password, an opaque token, or a case-sensitive path. An attractive method name should not conceal those assumptions. Decide which inputs the policy accepts and preserve the original text separately if users need to see it.
The sample assumes a nonnull string. Nullable annotations and runtime validation are separate concerns; a null receiver can still reach a static extension method if a caller supplies one. A reusable public method should choose whether null is rejected, preserved, or converted, and make that choice explicit in its contract.
Practice by adding a validation step that rejects an empty normalized identifier. Test whitespace-only input, mixed case, already normalized input, and a name containing internal spaces. Keep normalization and validation distinguishable so callers can tell whether text was transformed or rejected.
Place extension methods in a focused namespace rather than creating a global collection of unrelated helpers. Too many broadly named extensions make it harder to discover the actual API and can create confusing overload resolution. Prefer a small operation whose assumptions are clear at its call site and whose examples cover the relevant boundary cases.
A working example
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
Console.WriteLine(" rune ".NormalizeId());
Console.WriteLine("RUNE".NormalizeId());
static class IdExtensions {
public static string NormalizeId(this string value) {
return value.Trim().ToLowerInvariant();
}
}
Expected output
rune rune
An extension method is static code invoked with instance-style syntax.