Reputation: 433
Is there a better (nicer) way to write this if statement?
if(string1 == null && string2 == null && string3 == null && string4 == null && string5 == null && string6 == null){...}
Upvotes: 40
Views: 37407
Reputation: 109547
If you made a function like this:
public static bool AllNull(params string[] strings)
{
return strings.All(s => s == null);
}
Then you could call it like this:
if (AllNull(string1, string2, string3, string4, string5, string6))
{
// ...
}
Actually, you could change AllNull() to work with any reference type, like this:
public static bool AllNull(params object[] objects)
{
return objects.All(s => s == null);
}
2024 Update for .NET 9/ C# 13:
Now we have both nullable annotation and params collection, so we can write AllNull()
like so:
public static bool AllNull(params Span<object?> objects)
{
foreach (var obj in objects)
{
if (obj is not null)
return false;
}
return true;
}
This has the advantage that you can call it passing a list of reference objects without the compiler having to allocate an array and pass it to the AllNull()
method, which could be a lot more efficient.
You can also implement it using IEnumerable<object?>
:
public static bool AllNull(params IEnumerable<object?> objects)
{
return objects.All(s => s == null);
}
This allows even more collection types to be passed to it (but it will usually not allow the same level of optimisation as using Span<object?>
.)
Upvotes: 14
Reputation: 66399
In case you want to check null or empty, here is another way without arrays:
if (string.Concat(string1, string2, string3, string4, string5).Length == 0)
{
//all null or empty!
}
Upvotes: 2
Reputation: 98740
Well, I don't know if it is nicer or better, or not, you can use IEnumerable.Any method like this;
Determines whether a sequence contains any elements.
List<string> list = new List<string>{"string1","string2","string3", "string4", "string5"};
if(list.Any(n => n == null))
{
}
And you can use Enumerable.All() method like;
Determines whether all elements of a sequence satisfy a condition.
if (Enumerable.All(new string[] { string1, string2, string3, string4, string5 }, s => s == null) )
{
Console.WriteLine("Null");
}
Upvotes: 2
Reputation: 6434
string[] strs = new string[] { string1, string2, string3 };
if(strs.All(str => string.IsNullOrEmpty(str))
{
//Do Stuff
}
Or use strs.All(str => str == null)
if you don't want to check for empty strings.
Upvotes: 6
Reputation: 7523
This should do the same:
if (string.IsNullOrEmpty(string1 + string2 + string3 + string4 + string5 + string6)){...}
Upvotes: 1
Reputation: 460028
Perhaps using the null-coalescing operator(??
):
if((string1 ?? string2 ?? string3 ?? string4 ?? string5 ?? string6) == null){ ;}
If all strings are in a collection you can use Linq:
bool allNull = strings.All(s => s == null);
Upvotes: 65
Reputation: 3250
Make a IEnumerable of strings (list or array....), then you can use .All()
var myStrings = new List<string>{string1,string2,string3....};
if(myStrings.All(s => s == null))
{
//Do something
}
Upvotes: 3
Reputation: 32266
You could put all the strings in a list and use
if(listOfStrings.All(s=>s==null))
At the very least you can put it on multiple lines
if(string1 == null
&& string2 == null
&& string3 == null
&& string4 == null
&& string5 == null
&& string6 == null)
{...}
Upvotes: 15