DEV Community

Discussion on: Unhealthy Code: Null Checks Everywhere!

Collapse
amatiasq profile image
A. Matías Quezada • Edited on

I don't understand why do we need a whole class to create an array const myEmptyArray: string[] = EmptyArray.create<string>(); in C# you can just string[] myEmptyArray = new string[] {}; or List<string> myEmptyList = new List<string>();

Collapse
jamesmh profile image
James Hickey Author

Sure. There's a semantic preference and also, in some cases, a performance boost too.

In C#, for example, using Enumerable.Empty<T>() will use a singleton underneath the covers so you aren't allocating an empty array on the heap.

In very specific situations that could help. Otherwise, it's mostly a semantic preference.

Collapse
petergabriel2 profile image
PeterGabriel2

That enumerator was not good example. You can't compare mutable with immutable - in this case it is not about semantic.