Skip to content

Conversation

nogic1008
Copy link

@nogic1008 nogic1008 commented Oct 11, 2025

Closes #132

API breakdown (for new features)

This PR implements some throw helper methods via extension members when target project is C# 14 or above.

  • ArgumentException.ThrowIfNullOrEmpty(string? argument, string? paramName = default)
  • ArgumentException.ThrowIfNullOrWhiteSpace(string? argument, string? paramName = default)
  • ArgumentNullException.ThrowIfNull(object? argument, string? paramName = default)
  • ArgumentOutOfRangeException.ThrowIfEqual<T>(T value, T other, string? paramName = default)
  • ArgumentOutOfRangeException.ThrowIfNotEqual<T>(T value, T other, string? paramName = default)
  • ArgumentOutOfRangeException.ThrowIfGreaterThan<T>(T value, T other, string? paramName = default)
  • ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<T>(T value, T other, string? paramName = default)
  • ArgumentOutOfRangeException.ThrowIfLessThan<T>(T value, T other, string? paramName = default)
  • ArgumentOutOfRangeException.ThrowIfLessThanOrEqual<T>(T value, T other, string? paramName = default)
  • ObjectDisposedException.ThrowIf(bool condition, object instance)
  • ObjectDisposedException.ThrowIf(bool condition, Type type)

This implementation is based on the one in dotnet/runtime

Limitation

  • .NET Standard 2.0 does not have INumberBase<T> interface, so these methods are not implemented.
    • ArgumentOutOfRangeException.ThrowIfZero<T>(T value, string? paramName = null)
    • ArgumentOutOfRangeException.ThrowIfNegative<T>(T value, string? paramName = null)
    • ArgumentOutOfRangeException.ThrowIfNegativeOrZero<T>(T value, string? paramName = null)
  • Exception messages are not localized except ArgumentNullException.ThrowIfNull.
  • ArgumentNullException.ThrowIfNull cannot Tier0 intrinsic to avoid redundant boxing in generics because lack Intrinsic attribute.

- `PolySharp.OldCSharpVersion.Tests` (C# 13) does not generate polyfill
- `PolySharp.Tests` generates polyfill
@nogic1008 nogic1008 changed the title feat:/throwhelper extensions feat: polyfill some Exception.Throw... methods on C# 14 Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PolyFill Exception.Throw... methods using C# 14 static extension methods.

1 participant