2

Is there any way in C# to shorten this?

For example, how would you shorten this:

bool a = x == "YES" || x == "NO"

Code I'm writing:

public static bool isYESNO(string x)
{
        return x == "YES" || x == "NO";
}
Sky
  • 131
  • 5
  • 4
    Your method is already pretty short, unless you have 10 items for `OR`, that's it... – Ian Mar 23 '16 at 05:37
  • 1
    I would write it like this: `bool a = (x == "YES" || x == "NO");` IMO it's more readable since you can immediately see what's the condition and what's the assignment. – Zohar Peled Mar 23 '16 at 05:38
  • 2
    I really don't see what's wrong with your approach. At some point, attempts to make code more compact end up making the code difficult to read and sometimes less performant. You should try writing this function on a Turing machine and count your lucky stars that it's so clear and concise in C#. – paddy Mar 23 '16 at 05:47
  • Well, shorter does not mean better. All the posted answers cause additional memory allocations, which cannot be recommended as a good approach, and under a deper look they are more ugly than the initial code. – Diligent Key Presser Mar 23 '16 at 05:49
  • 1
    @Sky Be glad you don't need to use VB6, then :-) – Zohar Peled Mar 23 '16 at 05:49
  • I was unsatisfied with my original code because it looked a bit ugly, but Zohar's code was a bit clearer. I try to make my code as short as possible, which is probably a bad habit. – Sky Mar 23 '16 at 05:51
  • Short as possible doesn't mean less clear or less ugly. I agree that the code should be easy for read but it doesn't mean that it will be shorter. I think your original code is simpler. You may need to create an enum for it in case of multiple cases and in order to prevent for hard coding strings – ehh Mar 23 '16 at 05:58

3 Answers3

8

You can write the extension method:

public static class Extensions
{
    public static bool In<T>(this T item, params T[] items)
    {
         return items.Contains(item);
    }
}

And then use it like this:

var x = "YES";
bool a = x.In("YES", "NO");
Console.WriteLine(a);

It might be not too helpful if you have just couple of items, but it you have a lot of them it makes syntax shorter:

bool a = x.In("YES", "NO", "TRUE", "FALSE", "SOME OTHER VALUE");
dotnetom
  • 24,551
  • 9
  • 51
  • 54
3

You can try like

if (new[] {"YES", "NO"}.Contains(x))

This may be useful when you have multiple values to check in your OR condition. Something like

if (new[] {"YES", "NO", "value1", "value2", "value3", "value4"}.Contains(x))

In your present case your way of doing is quite readable and short.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Suppose you only want to check two items like what is shown in your example, I think your method is already pretty short. However, if you have a lot of items (like 10 items) for checking, consider using LINQ Contains for comparison:

string[] correctItems = new string[] { "YES", "NO", "bla1", "bla2", "bla3", "bla4", "bla5", "bla6", "bla7", "bla8" };
bool a = correctItems.Contains(x);
Ian
  • 30,182
  • 19
  • 69
  • 107