-
Notifications
You must be signed in to change notification settings - Fork 1k
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
in-keyword #316
Comments
Active patterns (coupled with Permit IEnumerable as the type of a params parameter) would completely cover this use case (and many, many more); public bool InPattern<T>(this IEnumerable<T> collection, T matchValue) =>
collection.Contains(matchValue);
...
if (id is In(1,2,3,4))
{
}
switch(id)
{
case In(1,2,3,4) : ...
} |
Active patterns are not extremely useful here a regular extension method works just fine, static bool In<T>(this T value, params T[] values) { .. }
if (value.In(1, 2, 3)) unless you want to use it in a OR patterns (#118) can also be helpful. |
@alrz I know it is nitpicking but the extensionmethod comes with a few "drawbacks" Apart from that you might need a reference to the assembly where that extensionmethod is located plus a using statement to make it avaible. |
C# really could use an
in
or alternativelyis in
keyword like other languages (e.g. TSQL) offer.It allways feels wrong writing code like
when this would feel / look so much better
The compiler should be able to generate something like the
switch
statement out of this so that there is no arraycreation involved like the extensionmethod does.Making it work for Enumerables is a bonus
where the compiler should just translate it into
Enumerable.Contains
I think this is connected to
Discussion: Range operator
#198The text was updated successfully, but these errors were encountered: