1

I have to implement a very big If and else statement in a short and smarter way.

for example :

If(seg.status =="hl"||seg.status =="hl2"||seg.status =="hl3"||seg.status =="hl4"||seg.status =="hl4"||seg.status =="hl5"||seg.status =="hl6"||seg.status =="hl7")

into small and smarter way

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    You could use the `In` extension method suggested here: https://stackoverflow.com/questions/271398/what-are-your-favorite-extension-methods-for-c-codeplex-com-extensionoverflow?answertab=votes#tab-top – Saragis Jul 24 '15 at 10:49
  • possible duplicate of [if statements matching multiple values](http://stackoverflow.com/questions/3907299/if-statements-matching-multiple-values) – dhh Jul 24 '15 at 10:50

6 Answers6

5

How about adding all your hl strings in a List<string> and check with Any and Contains like;

var list = new List<string>(){"hl", "hl1", "hl2", ...};
if(!list.Any(seg.status.Contains))

As Jon warned, this will return true if your string doesn't match with status, choose

if(list.Contains(seg.status))

instead.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 4
    That's an odd way of writing `if (list.Contains(seg.status))` (In fact, it's not quite doing the write thing at the moment... your condition would match a status of "boringstuffhlotherboringstuff" whereas the original condition wouldn't. – Jon Skeet Jul 24 '15 at 10:50
  • @JonSkeet Right Jon, sorry. Updated. – Soner Gönül Jul 24 '15 at 10:59
2

I would put them into a HashSet:

var keywords = new HashSet<String>(StringComparer.InvariantCultureIgnoreCase)
               {
                  "h1", "hl2", ...
               }

And then check if it contains the given word:

if(keywords.Contains(seg.status))
{
   // ToDo: What shall happen?
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • what would be the difference using list ? better performance ? – Tharif Jul 24 '15 at 11:03
  • 1
    @utility: Yes, just performance, but in case of 10 elements this doesn't matter. But IMHO the type `HashSet` also better describes for what these elements are used for: For fast lookup where order doesn't matter, a list instead would be my preference if order would matter. – Oliver Jul 24 '15 at 11:18
1

You could also use regex:

Regex.IsMatch(seg.status, @"hl\d*")
Breeze
  • 2,010
  • 2
  • 32
  • 43
0

You can try like this

var segList = new List<string>(){"hl", "hl1", "hl2","hl4", "hl15", "hl6","hl7"};

If(segList.contains(seg.status))
ssilas777
  • 9,672
  • 4
  • 45
  • 68
0

Do you do this in many places? If so, you could write a little string extension class to make the code more tidy:

public static class StringExt
{
    public static bool MatchesAnyOf(this string text, params string[] targets)
    {
        return targets.Any(target => string.Compare(text, target, StringComparison.OrdinalIgnoreCase) == 0);
    }
}

And you would call it like this:

if (seg.status.MatchesAnyOf("hl", "hl1", "hl2","hl4", "hl15", "hl6","hl7"))

(If you want a case-sensitive comparison, remove the StringComparison.OrdinalIgnoreCase parameter from string.Compare().)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

If you are confident on the pattern of the test variables naming:

using System.Text.RegularExpressions;

Regex re = new Regex("hl[1|2|3|4|5|6|7]");
string[] test=  {"hl1","hl2","hl3","hl4","hl5","hl6","hl7","hlasdfasdf"};
for(int i = 0; i < test.Length; i++){
    System.Console.WriteLine(test[i] + ":" + (re.IsMatch(test[i])?"match":"does not match"));
}

So in your case:

   if(re.IsMatch(seg.status)){ //...  }
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73