if (task1 != null)
//Do something with task1
else
{
if (task2 != null)
//Do something with task2
else
{
if (task3 != null)
//Do something with task3
else
{
if (task4 != null)
//Do something with task4
}
}
}
Is there an alternative to the above code? I’m looking for a sort of ‘flatter’ way of kind of switch casing on the tasks depending which is not null.
Thanks a LOT in advance for anyone that can help.
It depends if your method does anything else. If it doesn’t, you can use:
(I was about to add the same point that Marc was making – if you’re going to do the same thing with whichever task is first non-null, then the null-coalescing operator is indeed your friend.)