I believe everything 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", "brown" })) { bool bananaIsRipe = true; }
}
private static bool StringComparer(string source, string[] values)
{
foreach (var value in values)
{
if (source == value) return true;
}
return false;
}
Aucun commentaire:
Enregistrer un commentaire