Your array is not empty - it received 5 elements, each of which has a zero value.
Please note that it arrayitself is not null, nor is it query. But each element of them is null.
A truly empty array will 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 could be filtering null elements when copying an array:
Program[] query = array.Where(x => x != null)
.DefaultIfEmpty(new Program())
.ToArray();
EDIT: , DefaultIfEmpty? , . ; , . " ", :
Program[] query = array.Select(x => x ?? new Program())
.ToArray();
, Program ; Program , , :
Program defaultValue = new Program();
Program[] query = array.Select(x => x ?? defaultValue)
.ToArray();