0

I believe everytone knows an if clause with an 'or' in csharp:

bool bananaIsYellow = true;
bool bananaIsBrown = false;
if (bananaIsYellow || bananaIsBrown) bool bananaIsRipe = true;

the thing is that this can get very messy very fast as soon as you start to compare strings:

string bananaColor = yellow;
if (bananaColor == "yellow" || 
    bananaColor == "brown" ||
    bananaColor == "blue")
{
    bool bananaIsRipe = true;
}

Is there any way of shortening this?

the only way I would know is something like this (which is obviously not more beautiful or short):

string bananacolor = "yellow";
if (StringComparer(bananacolor, new string[] { "yellow", "brown", "blue" })) { bool bananaIsRipe = true; }
}
private static bool StringComparer(string source, string[] values)
{
    foreach (var value in values)
    {
        if (source == value) return true;
    }
    return false;
}
julian bechtold
  • 1,875
  • 2
  • 19
  • 49
  • Also, you shouldn't use `==` for comparing `string`s. Use `.Equals()` instead. `==` only compares reference, whereas `.Equals()` compares value. – absoluteAquarian Sep 25 '18 at 13:15
  • You can add you comparing values into array, and use `yourArray.Contains(bananaColor)`. – SᴇM Sep 25 '18 at 13:16
  • 1
    This is more of a preference of yours when writing your code. A `switch-case` block could maybe seem less of a visual pollution? – vinicius.ras Sep 25 '18 at 13:16
  • 2
    @Tau you probably have Java in your mind. In .Net the == operator for string is overloaded with an .Equals call. – thehennyy Sep 25 '18 at 13:20
  • Possible duplicate of [if statements matching multiple values](https://stackoverflow.com/questions/3907299/if-statements-matching-multiple-values) – SᴇM Sep 25 '18 at 13:21
  • @tau That's false. `==` compares the values of the strings, not the references. – Servy Sep 25 '18 at 13:23
  • @thehennyy Sorry, the auto complete put in the wrong username. – Servy Sep 25 '18 at 13:26
  • ah, my bad. i never really looked into it just now and assumed that my statement was true. – absoluteAquarian Sep 25 '18 at 13:27
  • @Tau: You can stay with `Equals` if you're more familiar with it, it has one advantage: you can use the overload that takes a `StringComparison`. You can't do that with `==` – Tim Schmelter Sep 25 '18 at 13:54

2 Answers2

6

You can use a collection and Enumerable.Contains:

string[] ripeColors = { "yellow", "brown", "blue" };
bool isBananaRipe = ripeColors.Contains(bananacolor);

If you want to compare in a case-insensitive manner:

bool isBananaRipe = ripeColors.Contains(bananacolor, StringComparer.InvariantCultureIgnoreCase);

By the way, blue bananas are ripe? Ew!

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

One option might be to use a regex with an alternation:

Regex regex = new Regex(@"^(yellow|brown|blue)$");
string bananaColor = "yellow";
Match match = regex.Match(bananaColor);

if (match.Success)
{
    Console.WriteLine(match.Value);
}

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Battle of the Tims, both having German sounding last names :-) – Tim Biegeleisen Sep 25 '18 at 13:18
  • True, i'm from germany, but Biegeleisen sounds like an american with german ancestors(maybe from Bügeleisen=flat iron) :D – Tim Schmelter Sep 25 '18 at 13:50
  • to be honest it was hard to decide whom to give the credits, both valid answers. although I can use Tim Schmelters answer also with other variabletypes which is why he made it. – julian bechtold Sep 25 '18 at 14:15
  • @TimSchmelter You're spot on brother, and in fact the Bügeleisen -> Biegeleisen transition happened at Ellis Island when those ancestors arrived. – Tim Biegeleisen Sep 25 '18 at 14:27
  • @julianbechtold, while both answers work. I would also take into account the readability and speed of each. For some, Regex is harder to read and maintain. But, in the case of many possible values, it may be faster. Food for thought. – Mikanikal Sep 25 '18 at 14:31
  • @Mikanikal this is not true from my point of view. you can also for example write 'bool bananaIsRipe = Regex.Match(banana, @"[yellow][brown][blue]").success()' – julian bechtold Sep 25 '18 at 14:34
  • A regex based answer really starts to kick in over the accepted answer when we want to match a series of patterns, rather than exact strings. – Tim Biegeleisen Sep 25 '18 at 14:55
  • What is not true? I wasn't making a blanket statement. I was simply giving a pro/con of using Regex over the accepted answer. I personally like this answer, I was the first up-vote :) – Mikanikal Sep 25 '18 at 16:13