[Proposal] Add == operator with multi check #2947
-
For now i need to write
Very usefull will be short syntax
Or something like that to make quick check if variable is on of the choice. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
To be consistent with +=, *=, ??= etc. That being said, if or patterns are added they would solve this problem for constant s: |
Beta Was this translation helpful? Give feedback.
-
Yes. May be just
|
Beta Was this translation helpful? Give feedback.
-
I rewrite upper text. |
Beta Was this translation helpful? Give feedback.
-
Have you considered that this can be achieved (for your specific example) with an extension method? using System;
using System.Linq;
public static class IntExtensions {
public static bool EqualsAnyOf(this int value, params int[] values)
=> values.Any(i => i == value);
}
public class Demo
{
public void Main(int value)
{
if (value.EqualsAnyOf(5, 12, 101))
{
// elided
}
}
} More generally, you should check out #2850 (Proposed changes for Pattern Matching in C# 9.0 - Draft Specification) as the syntax proposed there will do exactly what you are requesting. |
Beta Was this translation helpful? Give feedback.
-
And you can make that a generic method, not just for int, and it covers all
bases.
…On Sun, 10 Nov 2019, 06:42 Bevan Arps, ***@***.***> wrote:
Have you considered that this can be achieved
<https://sharplab.io/#v2:D4AQTAjAsAUCAMACEEB0AZAlgOwI4G5ZYQBmZCANmTEQElsAXAUQA8GBTbAZ0wHtvEAb1iJRyMiioAjXrwA2iJrgCuAQzlcAgtgCeAeQBmACgYALTF0Q4GiAG7rl7ADSIADqoBOqgLaXrAbQBdOwd2LgBKETFogF4APhC5Ry5UbR0jTER4qyyYxMdwwhgAXyI4CRoAEXZvXkRYYRho0mQAFkQAWVUcDMZ89kimsUbo6MwDRCN7JPZUJTUNNMMjAFYXCDB1+Ahw8Pqh0aEow+iAelPEdjlMABN2G+PR0oPEZ+KgA=>
(for your specific example) with an extension method?
using System;using System.Linq;
public static class IntExtensions {
public static bool EqualsAnyOf(this int value, params int[] values)
=> values.Any(i => i == value);
}
public class Demo
{
public void Main(int value)
{
if (value.EqualsAnyOf(5, 12, 101))
{
// elided
}
}
}
More generally, you should check out #2850
<#2850> (*Proposed changes for
Pattern Matching in C# 9.0 - Draft Specification*) as the syntax proposed
there will do exactly what you are requesting.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/csharplang/issues/2947?email_source=notifications&email_token=ADIEDQN3HWO4NNOHTWIL7KDQS6ULRA5CNFSM4JLJAVKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDUWVGI#issuecomment-552168089>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQNRYDFIAPBU5VTZSK3QS6ULRANCNFSM4JLJAVKA>
.
|
Beta Was this translation helpful? Give feedback.
-
I would really prefer to see an “in” operator that was essentially an alias for “contains”. If(x in {1, 2, 3} ) { |
Beta Was this translation helpful? Give feedback.
-
This is likely to be implemented with "and, or, and not patterns" #1350. if (x is 1 or 2 or 3)
{
//...
} |
Beta Was this translation helpful? Give feedback.
This is likely to be implemented with "and, or, and not patterns" #1350.