Your array isn't empty - it's got 5 elements, each of which has a null value.
Note that array
itself isn't null - and neither is query
. But each element of them is null.
A truly empty array would have 0 elements:
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
Program[] array = new Program[0];
Program[] query = array.DefaultIfEmpty(new Program()).ToArray();
foreach (var item in query)
{
Console.WriteLine(item.ToString());
}
Console.ReadKey();
}
}
Another alternative would be to filter null elements while you're copying the array:
Program[] query = array.Where(x => x != null)
.DefaultIfEmpty(new Program())
.ToArray();
EDIT: Perhaps you misunderstood the purpose of DefaultIfEmpty
? This works on the whole sequence, not on an individual element basis. The idea isn't to replace null elements with a default value; it's to make sure that the result sequence is never empty. If you want to do a simple "replace null with a new value" you can do:
Program[] query = array.Select(x => x ?? new Program())
.ToArray();
Note that this will create a new value of Program
for each null element; if you only want to create a single instance of Program
and use multiple references to that, one for each originally null element, you could do this:
Program defaultValue = new Program();
Program[] query = array.Select(x => x ?? defaultValue)
.ToArray();