You must loop through a array as a parameter, using params
, and sweep it all into a loop, making the if
you want.
If you do not care about performance you can do with LINQ in a row.
Depending on what you want to do, the logic would be a bit different .
If it really is to receive multiple fields from an object then it would be better to pass the object and scan. There you would either have to do item by item or use reflection , which would make the code a lot slower, probably just for save typing, and it is not simple to select fields, they would all be used, but with a slightly more complicated logic, you would need to see if it makes up for it. Need to generalize.
using static System.Console;
using System.Linq;
public class Program {
public static void Main() {
WriteLine(Teste("João", "048", "abc"));
WriteLine(Teste("João", "048", "abc", ""));
WriteLine(Teste("", "João", "048", "abc"));
WriteLine(Teste("", null));
WriteLine(Teste2("", null));
WriteLine(Teste2("", null, "João", "048", "abc"));
}
public static bool Teste(params string[] textos) {
foreach (var item in textos) if (!string.IsNullOrWhiteSpace(item)) return true;
return false;
}
public static bool Teste2(params string[] textos) => textos.Any(item => !string.IsNullOrWhiteSpace(item));
}
I took advantage of and converted the code to C # style.