3

How can I make this shorter?

string veryLongVariableName;

if (veryLongVariableName == "a" || veryLongVariableName == "b" || veryLongVariableName == "c"|| veryLongVariableName == "d"|| veryLongVariableName == "e"|| veryLongVariableName == "f")

something like this possible?

if (veryLongVariableName == ("a", "b", "c", "d", "e", "f"))
if (veryLongVariableName == ("a" || "b" || "c" || "d" || "e" || "f"))
andrewc
  • 41
  • 2
  • 2
    Deos [this](https://stackoverflow.com/questions/16866174/check-if-a-variable-is-in-an-ad-hoc-list-of-values) answer your question? – Sweeper Mar 13 '20 at 07:40

3 Answers3

6

You nearly had it

if (new [] {"a", "b", "c", "d", "e", "f"}.Contains(veryLongVariableName))

Note : This does allocate every time you call it

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • a: should that be `Contains`, not `Any`? and b: this is pretty allocatey - fine for some things, but probably not good advice in the general case – Marc Gravell Mar 13 '20 at 07:45
  • @MarcGravell yeah it got a bit typey for 5pm on a friday, and you a right, its not the bastion of great coding. ill make a note – TheGeneral Mar 13 '20 at 07:46
  • 2
    the allocation could be avoided by creating it as a static array somewhere, note; but... that has pros and cons – Marc Gravell Mar 13 '20 at 07:50
  • thank you for swift answers. i was too wondering if it is worth doing this for a little bit of clarity – andrewc Mar 13 '20 at 07:51
2

At risk of making your long line longer, I'd probably go with a switch here:

switch (veryLongVariableName)
{
    case "a:
    case "b:
    case "c:
    case "d:
    case "e:
    case "f:
        // your stuff 
        break;
}

(a perhaps a utility IsSomething(...) method that returns true or false on the condition, and just an if (IsSomething(...)) {...} in the code shown.

Reasons: it is clear, obvious, and efficient (no allocations; optimized by the compiler).

As an example for the IsSomething:

static bool IsSomething(string theThing) => theThing switch {
    "a" => true,
    "b" => true,
    "c" => true,
    "d" => true,
    "e" => true,
    "f" => true,
    _ => false,
};
// ...
if (IsSomething(veryLongVariableName)) { ... }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

You can have all your string conditions in a List, then:

var match = myList
    .FirstOrDefault(x => x.Contains(veryLongVariableName));

if(match != null)
    //Do stuff
Raynoceros
  • 386
  • 2
  • 15
  • 1
    that's a *really* expensive way to write a "contains" check; that allocates a capture context and delegate every time... and I'm not even sure it does the same thing - this looks kinda like a nested (jagged) scenario? – Marc Gravell Mar 13 '20 at 07:54
  • Yes agree, but it's all depends on what the OP really want. Compiler friendly or Code tidiness. – Raynoceros Mar 13 '20 at 08:05
  • that speaks to the "expensive" part (although I'd argue that it is *needlessly* expensive here); it doesn't speak to the "same thing" part – Marc Gravell Mar 13 '20 at 08:26