Trying to rewrite this using LINQ:
if (mode != "A" && mode != "B" && mode != "C" && mode != "D" && mode != "E" && mode != "F" && mode != "G")
{
continue;
}
What would be the most clear and concise way to refactor this? I could have sworn I'd seen a post like this before but I cannot find it at the moment.
You can use Contains
method from IList<T>
:
IList<string> modes = new[]{"A","B","C","D","E","F","G"};
if (!modes.Contains(mode))...
Write an extension method for the string class
public static bool In(this string s, params string[] values)
{
return values.Any(x => x.Equals(s));
}
call it in this way
if (!mode.In("A", "B", "C", "D","E","F", "G")
{
continue;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With