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

Multiple string null and empty check #108565

Open
aliajboy opened this issue Oct 2, 2024 · 6 comments
Open

Multiple string null and empty check #108565

aliajboy opened this issue Oct 2, 2024 · 6 comments
Labels
area-System.Runtime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@aliajboy
Copy link

aliajboy commented Oct 2, 2024

It is not a bug. It is a suggestion or feature request

I want to use IsNullOrEmpty() here:
if (!string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName) && !string.IsNullOrEmpty(Mobile))
It is not nice to do like this.

It would be nice to have something like IsNullOrEmpty(string,string,string,string...)
something like Path.Combine(). As you know it accepts some string and make the path. so, we could have 1-5 string in IsNullOrEmpty method, too.

thanks

Copy link

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.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Oct 2, 2024
Copy link

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.

@KalleOlaviNiemitalo
Copy link

Seems easy enough to implement in the application:

public class C {
    public static bool IsAnyNullOrEmpty(params string?[] strings) {
        foreach (string? s in strings) {
            if (string.IsNullOrEmpty(s)) {
                return true;
            }
        }
        
        return false;
    }
}

This costs a string[] allocation for each call, though. dotnet/csharplang#7700 would allow params ReadOnlySpan<string?> strings to fix that. In addition, overloads could be added for a small number of strings.

I don't remember ever needing that kind of multiple IsNullOrEmpty check in an application. If it is for detecting invalid inputs, then the error message would typically explain which of the strings is invalid, like in ArgumentException.ThrowIfNullOrEmpty. If the strings are in properties rather than parameters, then OptionsValidatorAttribute may be useful.

@aliajboy
Copy link
Author

aliajboy commented Oct 4, 2024

I know we have different approaches for this. but as a suggestion, it would be nice to have such feature in future.
thanks

@marcpopMSFT marcpopMSFT transferred this issue from dotnet/sdk Oct 4, 2024
@HaloFour
Copy link

HaloFour commented Oct 4, 2024

This is an API request, belongs in https://github.com/dotnet/runtime

@333fred 333fred transferred this issue from dotnet/csharplang Oct 4, 2024
@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 Oct 4, 2024
@vcsjones vcsjones 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 Oct 5, 2024
Copy link
Contributor

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

@jeffhandley jeffhandley added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed untriaged New issue has not been triaged by the area owner labels Oct 8, 2024
@jeffhandley jeffhandley added this to the Future milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Runtime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

5 participants