Closed as duplicate of#86830
Closed as duplicate of#86830
Description
Background and motivation
throwing if a collection or span is empty
API Proposal
namespace System;
public class ArgumentException : SystemException
{
public static void ThrowIfNullOrEmpty<T>([NotNull] T? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) where T : System.Collections.ICollection
{
ArgumentNullException.ThrowIfNull(argument, paramName);
if (argument.Count == 0)
{
throw new ArgumentException("The value cannot be an empty collection.", paramName);
}
}
public static void ThrowIfEmpty<T>(ReadOnlySpan<T> argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument.IsEmpty)
{
throw new ArgumentException("The value cannot be an empty span.", paramName);
}
}
}
API Usage
List<string> values = [];
ArgumentException.ThrowIfNullOrEmpty(values);
Alternative Designs
could use static extension members to add this feature without changing ArgumentException
Risks
No response