Skip to content
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 for collection #86830

Closed
WeihanLi opened this issue May 27, 2023 · 3 comments
Closed

[API Proposal]: ArgumentException.ThrowIfNullOrEmpty for collection #86830

WeihanLi opened this issue May 27, 2023 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime

Comments

@WeihanLi
Copy link
Contributor

WeihanLi commented May 27, 2023

Background and motivation

Sometimes, we may also need to check if a collection is empty just likes the string.IsNullOrEmpty

for example:

if (HostProperties.HostConfigs is null || HostProperties.HostConfigs.Count == 0)
throw new Exception($"no perHostConfigs found");

https://github.com/Azure/azure-sdk-for-net/blob/aea202adfea6f1dbb117ab9f8f3cde7028d8d2d1/sdk/core/Azure.Core/src/Shared/Argument.cs#L82-L84

API Proposal

namespace System;

public class ArgumentException : SystemException
{
    public static void ThrowIfNullOrEmpty<T>([NotNull]ICollection<T>? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
    {
        ArgumentNullException.ThrowIfNull(argument, paramName);
        if (argument.Count == 0)
        {
            throw new ArgumentException("The collection is empty...", paramName);  
        }
    }
}

API Usage

var arr = Array.Empty<int>();
ArgumentException.ThrowIfNullOrEmpty();

// throwing the ArgumentException out

Alternative Designs

No response

Risks

No response

@WeihanLi WeihanLi added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 27, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 27, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 27, 2023
@jkotas
Copy link
Member

jkotas commented May 27, 2023

This was discussed and rejected in #62628 (comment) : We don't believe we need an overload of ThrowIfNullOrEmpty for collections as this feels rare.

@danmoseley danmoseley added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 27, 2023
@ghost
Copy link

ghost commented May 27, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Sometimes, we may also need to check if a collection is empty just likes the string.IsNullOrEmpty

for example:

if (HostProperties.HostConfigs is null || HostProperties.HostConfigs.Count == 0)
throw new Exception($"no perHostConfigs found");

https://github.com/Azure/azure-sdk-for-net/blob/aea202adfea6f1dbb117ab9f8f3cde7028d8d2d1/sdk/core/Azure.Core/src/Shared/Argument.cs#L82-L84

API Proposal

namespace System;

public class ArgumentException : SystemException
{
    public static void ThrowIfNullOrEmpty<T>([NotNull]ICollection<T>? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
    {
        ArgumentNullException.ThrowIfNull(argument, paramName);
        if (argument.Count == 0)
        {
            throw new ArgumentException("The collection is empty...", paramName);  
        }
    }
}

API Usage

var arr = Array.Empty<int>();
ArgumentException.ThrowIfNullOrEmpty();

// throwing the ArgumentException out

Alternative Designs

No response

Risks

No response

Author: WeihanLi
Assignees: -
Labels:

api-suggestion, area-System.Runtime, untriaged

Milestone: -

@stephentoub
Copy link
Member

stephentoub commented May 27, 2023

Also here:
#77749 (comment)
If we felt it was valuable, we could do it just for arrays, but using the interfaces to make it more general, I'm concerned we're actually creating a perf pit of failure.

@WeihanLi WeihanLi closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label May 28, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

4 participants