Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better Syntax for an If statement

I have a condition like this one:

if (string.IsNullOrEmpty(filename) || size != "Large" || size != "Medium" || size != "Small")

Probability in future I will have to manage more size in the if statement.

I would like to know if exist a more manageable and readable way to write this condition.

Please provide a real example thanks for your time on this.

like image 795
GibboK Avatar asked Dec 03 '22 07:12

GibboK


1 Answers

You can keep a hashset of words and check:

HashSet<string> filterWords = new HashSet<string>();
// Put all words in the hash set


if (filterWords.contains(size))
{
    // Do what ever you need
}
like image 72
Lior Ohana Avatar answered Dec 14 '22 07:12

Lior Ohana