BlackVikingPro BlackVikingPro - 23 days ago 15
C# Question

Searching Arrays with Strings from other Arrays C#

I have this thing for checking if the client entered some help command in the arguments while triggering the console application I've built in C#. Below is my current code to complete this task:

string[] help = { "-h", "--help", "/?", "help", "-help", "*help*" };
if (args.Contains(help[*])
{
Usage(); // prints out application usage
Environment.Exit(1); // exits console application only
}


I originally used the following, which worked, but I wanted to clean the code up a little bit:

if (args.Contains("-h") || args.Contains("--help") || args.Contains("/?") ...)
{
Usage();
Environment.Exit(1);
}


How would I perform the task I'm trying to perform in the cleanest coded way possible?

Thanks in advance :)

Answer

How about args.Any(arg => help.Contains(arg))?

Comments