Proposal: switch ignore case #5551
Replies: 19 comments 4 replies
-
How would this work? |
Beta Was this translation helpful? Give feedback.
-
Why not just |
Beta Was this translation helpful? Give feedback.
-
It would seem like a more generally useful feature (but still one that was just syntax sugar) would be to allow:
|
Beta Was this translation helpful? Give feedback.
-
@tannergooding How about |
Beta Was this translation helpful? Give feedback.
-
@tannergooding , string switches generate a dictionary last I checked, so it should be possible to change the dictionary to have a Also, if you're going to do the normalization approach, you should do |
Beta Was this translation helpful? Give feedback.
-
Active patterns would solve this issue, although the syntax might look a little different, e.g. switch(s)
{
case IgnoreCase("some_value1"):
break;
case IgnoreCase("some_value2"):
break;
} |
Beta Was this translation helpful? Give feedback.
-
I would prefer to leave the public static string FizzBuzz(int n)
{
switch(i % 3, i % 5)
{
case (0, 0): return "fizzbuzz";
case (0, _): return "fizz";
case (_, 0): return "buzz";
default: return n.ToString();
}
} |
Beta Was this translation helpful? Give feedback.
-
@tannergooding |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@vladd, right. Looks like
In either case, I would still think supporting a more general syntax would be desirable:
That makes it more generally useful and still handles the desired case. |
Beta Was this translation helpful? Give feedback.
-
For simple switch (person, StringComparer.OrdinalIgnoreCase) {
case { LastName is "Gates" }:
// matches when LastName is "gates" or "GATES"?
break;
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Dictionary can use
|
Beta Was this translation helpful? Give feedback.
-
Any status on this? If
For me it would seem to a good addition to the language |
Beta Was this translation helpful? Give feedback.
-
I don't think this was mentioned previously, so here goes: not only there are issues with PS. Right after I wrote this, I found one comment above which mentioned allocation :) Oh well, I'll just leave it be. |
Beta Was this translation helpful? Give feedback.
-
Here comes a C# syntax proposal for
switch (text) by StringComparison.OrdinalIgnoreCase
{
case "some_value1":
break;
case "some_value2":
break;
}
switch (text) by StringComparer.OrdinalIgnoreCase
{
case "some_value1":
break;
case "some_value2":
break;
}
int token =
text switch by StringComparison.OrdinalIgnoreCase
{
"some_value1" => 0,
"some_value2" => 1
};
int token =
text switch by StringComparer.OrdinalIgnoreCase
{
"some_value1" => 0,
"some_value2" => 1
}; The |
Beta Was this translation helpful? Give feedback.
-
Maybe this problem will be solved via " explicit extension StringWithCasingComparisonExtension for ValueTuple<string, StringComparison>
{
// NOTE: In fact I'm not familiar with extension declaration syntax.
// I think it may be written like this :)
public static bool operator is(string s)
=> this.Item1.Equals(s, this.Item2);
} Then: (string, StringComparison) s = ("Value_To_Be_Compared", StringComparison.OrdinalIgnoreCase);
if (s is "value_to_be_compared")
// ... or if (("Value_To_Be_Compared", StringComparison.OrdinalIgnoreCase) is "value_to_be_compared")
// ... or switch ("Value_To_Be_Compared", StringComparison.OrdinalIgnoreCase)
{
case "value_to_be_compared":
// ...
break;
} |
Beta Was this translation helpful? Give feedback.
-
This syntax improvement would be so amazing |
Beta Was this translation helpful? Give feedback.
-
Accepting But how would we translate this to switch expressions? Switch expressions don't need braces, so Maybe |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently switch statement on strings is case-sensitive.
Consider a way to provide an ignore-case switch construct so it could be optimized by compiler.
My example proposal:
Beta Was this translation helpful? Give feedback.
All reactions