I implemented Array.Empty<T>
because want to use this code
foreach (Control c in controls.OrEmptyIfNull())
{
this.Controls.Add(c);
}
OrEmptyIfNull extension method
but my program is using .NET version 4.5 and I don't want to change.
so coded below
public static class ExtensionClass
{
public static T[] Empty<T>(this Array array)
{
return EmptyArray<T>.Value;
}
public static IList<T> OrEmptyIfNull<T>(this IList<T> source)
{
return source ?? Array.Empty<T>();
}
internal static class EmptyArray<T>
{
public static readonly T[] Value = new T[0];
}
}
but an error occurred this line return source ?? Array.Empty<T>();
Error CS0117 'Array' does not contain a definition for 'Empty'
how to use Array.Empty()
or check before foreach List is null much prettier