Ever since I discovered the ?? colaesce operator I embraced it with full force.
Today I discovered another usefull use:
// this:
string[] arrayOfStrings1 = new string[] { "arr1s1", "arr1s2", "arr1s3" };
foreach (string str in (arrayOfStrings1 ?? new string[0]))
{
// do some stuff on the array elements
}
// equals to this:
string[] arrayOfStrings2 = new string[] { "arr2s1", "arr2s2", "arr2s3" };
if (arrayOfStrings2 != null)
{
foreach (string str in arrayOfStrings2)
{
// do some stuff on the array elements
}
}
// in terms of what it does
This enables me to get rid of all "if null" checks. I looked at IL with MSIL dissasembler and it doesn't look like there's any penalty in performance with using this so i'll stick with it. Of course reading IL isn't one of my strong sides so maybe someone can prove me wrong. :) But performance in this case doesn't outweigh the removal of "if null" check which i really hate.