-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Runtime
Milestone
Description
Background and motivation
Inspired by ArgumentNullException.ThrowIfNull(object)
. string.IsNullOrEmpty(text)
is a frequent check especially in web/Console project. At first I thought InvalidDataException
would be the best name but it is in System.IO
namespace, so I think the closest should be ArgumentException
so user doesn't need to import that namespace.
Now:
if (string.IsNullOrEmpty(input)) { throw new ArgumentException($"{nameof(input)} must contain value."); }
If the API is approved:
ArgumentException.ThrowIfNullOrEmpty(input);
While we are at it, personally I rarely use it but maybe ThrowIfNullOrWhiteSpace
may be useful for others as well?
API Proposal
namespace System
{
public class ArgumentException : SystemException
{
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression("argument")] string? paramName = null)
}
}
API Usage
ArgumentException.ThrowIfNullOrEmpty(input);
Alternative Designs
No response
Risks
No response
Mrxx99, WeihanLi, CDuke and antoniofreire
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Runtime