Skip to content

Instantly share code, notes, and snippets.

@danielmackay
Created October 11, 2023 06:01
Show Gist options
  • Save danielmackay/7aef664a57e2740bbdde891bda86d86e to your computer and use it in GitHub Desktop.
Save danielmackay/7aef664a57e2740bbdde891bda86d86e to your computer and use it in GitHub Desktop.
Guard Clause Custom Extension
public static class StringLengthGuard
{
public static string StringLength(this IGuardClause guardClause,
string input,
int maxLength,
[CallerArgumentExpression("input")] string? parameterName = null)
{
if (input?.Length > maxLength)
throw new ArgumentException($"Cannot exceed string length of {maxLength}", parameterName);
return input!;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment