-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: ArgumentException.ThrowIfNullOrWhitespace #76529
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivation.NET 7 introduced a new helper method: In lots of use-case scenarios an empty strings is equal to a string which purely consist out of whitespaces. Let's say a GitHub username is invalid either way: Null, empty or full of blanks or tabs. There are lots of scenarios where user input is only valid if there are printable characters. To remove the boiler plate code and align how user would write code a new API Proposalpublic class ArgumentException
{
public static void ThrowIfNullOrWhitespace(string argument, [CallerArgumentExpression("argument")] string? paramName = null)
{
if (string.IsNullOrWhitespace(argument))
{
ArgumentNullException.ThrowIfNull(argument, paramName);
throw new ArgumentException("String should not be empty or have only whitespaces", paramName);
}
}
} API Usagepublic void AddUser(string? username)
{
ArgumentException.ThrowIfNullOrWhitespace(username);
} Alternative DesignsNo response RisksNo response
|
This is certainly a reasonable piece of logic to have and I understand the desire to have a helper method for this. That being said, this is a very specific use case that isn't common enough across the .NET ecosystem to warrant a built-in helper. All kinds of variations of this kind of logic come to mind. I solve the "whitespace problem" in user input like this: var input = ...;
var trimmed = input.Trim();
if (trimmed.Length == 0) ... //Error case
UseUserInput(trimmed); //Reuse the trimmed string for further processing This is the form of logic that my projects have needed many times. My logic is slightly different from yours, making my point. I have a helper method just for this, and it would not have any business being in the BCL because it is overly specific. |
Fair point and I do understand your reasoning. That said, we can close the proposal. |
Background and motivation
.NET 7 introduced a new helper method:
ArgumentException.ThrowIfNullOrEmpty()
, which throws anArgumentNullException
when the string is null andArgumentException
when the string is empty.In lots of use-case scenarios an empty strings is equal to a string which purely consist out of whitespaces. Let's say a GitHub username is invalid either way: Null, empty or full of blanks or tabs. There are lots of scenarios where user input is only valid if there are printable characters.
To remove the boiler plate code and align how user would write code a new
ThrowIfNullOrWhitespace
could be introduced.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: