How to guard against "not one of" #230
Unanswered
patrickiel
asked this question in
Q&A
Replies: 1 comment
-
Currently, there is a Guard.Against.InvalidInput(city, nameof(city), c => new [] {"Berlin", "New York", "Paris" }.Any(x => x.Equals(c))); OR: Guard.Against.InvalidInput(city, nameof(city), c => c is "Berlin" or "New York" or "Paris", "Hi"); Also, it not that hard to define your own extension method: public static class GuardClauseExtensions
{
public static T NotOneOf<T>(this IGuardClause g, T input, IEnumerable<T> values, string parameterName, string message = null)
{
if (values.Any(v => v.Equals(input)))
{
throw new ArgumentException(string.IsNullOrWhiteSpace(message) ? "The argument value is not allowed" : message, parameterName);
}
return input;
}
} Now you have your very own: Guard.Against.NotOneOf(city, new [] {"Berlin", "New York", "Paris" }, nameof(city)); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there already a way to check if an element occurs in a collection, something like:
Beta Was this translation helpful? Give feedback.
All reactions