-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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.ThrowIfNullOrEmpty(string) #62628
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 motivationInspired by 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 API Proposalnamespace System
{
public class ArgumentException : SystemException
{
public static void ThrowIfNullOrEmpty([NotNull] string argument, [CallerArgumentExpression("argument")] string? paramName = null)
}
} API UsageArgumentException.ThrowIfNullOrEmpty(input); Alternative DesignsNo response RisksNo response
|
... but the parameter can be null, since that's the point of the exception? |
The argument to the existing [NotNull] object? argument which uses |
Makes sense to me. It would collapse stuff like runtime/src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs Lines 44 to 52 in dfbae37
from 10 lines to 1. In that example, it has a custom message, ie "Empty name is not legal.", and using this proposed API would change that to something generic. I think that's totally fine. There are cases where perhaps the message adds slightly more value eg "Empty file name is not legal." and perhaps those would not change to use this API, which is fine. |
Maybe it would also make sense to add overload with public static void ThrowIfNullOrEmpty<T>([NotNull] I/*ReadOnly*/Collection<T> argument, [CallerArgumentExpression("argument")] string? paramName = null); Usages: runtime/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs Lines 264 to 265 in 895c99c
Lines 84 to 85 in 895c99c
Didn't try to find splitted
|
If this is done, then after that I'm sure someone will ask: "What about an |
namespace System
{
public partial class ArgumentException
{
public static void ThrowIfNullOrEmpty([NotNull] string? argument,
[CallerArgumentExpression("argument")] string? paramName = null);
}
} |
Guys check suggestion about ICollection above please :) |
@hrrrrustic I edited my comment. |
@datvm do you want to add the API now? |
@danmoseley awesome, please go ahead |
To conform to the string overloads, it would also make sense to create an overload for IsNullOrWhiteSpace. namespace System
{
public partial class ArgumentException
{
public static void ThrowIfNullOrEmpty([NotNull] string? argument,
[CallerArgumentExpression("argument")] string? paramName = null);
public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument,
[CallerArgumentExpression("argument")] string? paramName = null);
}
} |
Do you find yourself ever checking against whitespace only? |
Only against whitespace actually not. But checking against null or whitespace is a common practice. Even if there are not many use cases for this case with me, I think imo it would be good to add this case anyway for completeness. |
I wonder what's the API designer team philosophy on this. From the video I watched, it seems they prefer to have only very needed APIs added instead of "completeness". I understand and agree with this approach since .NET now can be used in Web environment through WebAssembly as well so they probably want the standard library to be as small as possible. You should check the above video, they discussed about the possibilities of adding other throw helpers as well. |
But that would change the exception behavior, since an Or should the proposal throw an |
You're right about that. |
As noted above, it should throw ANE (which is-a AE) if the argument is null. |
@datvm I meant, are you interested in making the PR? No obligation, it's just that you opened the issue so asking you first. If anyone else is interested, they're welcome too. We have full instructions for how to contribute and can help as much as you need. |
@danmoseley ah thanks for clarifying. I will leave this one for someone else. I will try next time when I have studied an example pull request. |
@datvm sounds good, if you are interested there are issues with the 'easy' label and perhaps there's one you might be interested to start with. Anyone else interested in offering a PR here? |
Doesn't seem like a big deal. If it's nothing urgent, I'm happy to do it. |
Related: dotnet/roslyn#59094 |
Background and motivation
Inspired by
ArgumentNullException.ThrowIfNull(object)
.string.IsNullOrEmpty(text)
is a frequent check especially in web/Console project. At first I thoughtInvalidDataException
would be the best name but it is inSystem.IO
namespace, so I think the closest should beArgumentException
so user doesn't need to import that namespace.Now:
If the API is approved:
While we are at it, personally I rarely use it but maybe
ThrowIfNullOrWhiteSpace
may be useful for others as well?API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: