Skip to content

[API Proposal]: Add ArgumentOutOfRangeException.ThrowIfNotZero #84415

@xtqqczze

Description

@xtqqczze

Background and motivation

We already have ArgumentOutOfRangeException.ThrowIfZero.

API Proposal

public class ArgumentOutOfRangeException : ArgumentException
{
  public static void ThrowIfZero<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
    where T : INumberBase<T>
  {
    if (!T.IsZero(value))
      ThrowNotZero(paramName, value);
  }
}

API Usage

internal sealed class SingleItemReadOnlyList : IList
{
  private readonly object? _item;
  
  public SingleItemReadOnlyList(object? item) => _item = item;
  
  public object? this[int index]
  {
    get
    {
      ArgumentOutOfRangeException.ThrowIfNotZero(index);
      return _item;
    }
  }
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions