Skip to content

sanitizeCSharpIdentifier

Transforms an arbitrary string into a valid C# identifier by replacing invalid characters. The result may still be a C# keyword — callers should combine with keyword escaping if needed.

  • If the first character is not a letter or underscore, a _ prefix is added.
  • Subsequent non-word characters are replaced with _.
  • Empty strings become _.
import { sanitizeCSharpIdentifier } from "@alloy-js/csharp";
function sanitizeCSharpIdentifier(name: string): string;
namestring

string A string that satisfies C# identifier character rules.