I have some code where I\'m returning an array of objects.
Here\'s a simplified example:
string[] GetTheStuff() {
List s = null;
The upcoming version 4.6 of .NET (later in 2015) contains a static method returning a length-zero string[]
:
Array.Empty<string>()
I suppose it returns the same instance if called many times.
Yes, as others have said, the empty array takes up a few bytes for the object header and the length field.
But if you're worried about performance you're focusing on the wrong branch of execution in this method. I'd be much more concerned about the ToArray call on the populated list which will result in a memory allocation equal to its internal size and a memory copy of the contents of the list into it.
If you really want to improve performance then (if possible) return the list directly by making the return type one of: List<T>, IList<T>, ICollection<T>, IEnumerable<T>
depending on what facilities you need from it (note that less specific is better in the general case).
Others have answered your question nicely. So just a simple point to make...
I'd avoid returning an array (unless you can't). Stick with IEnumerable and then you can use Enumerable.Empty<T>()
from the LINQ APIs. Obviously Microsoft have optimised this scenario for you.
IEnumerable<string> GetTheStuff()
{
List<string> s = null;
if (somePredicate())
{
var stuff = new List<string>();
// load data
return stuff;
}
return Enumerable.Empty<string>();
}
This is not a direct answer to your question.
Read why arrays are considered somewhat harmful. I would suggest you to return an IList<string> in this case and restructure the code a little bit:
IList<string> GetTheStuff() {
List<string> s = new List<string>();
if( somePredicate() ) {
// imagine we load some data or something
}
return s;
}
In this way the caller doesn't have to care about empty return values.
EDIT: If the returned list should not be editable you can wrap the List inside a ReadOnlyCollection. Simply change the last line to. I also would consider this best practice.
return new ReadOnlyCollection(s);
Declared arrays will always have to contain the following information:
This would most likely be trivial, but for higher numbers of dimensions and higher lengths it will have a performance impact on loops.
As for return types, I agree that an empty array should be returned instead of null.
More information here: Array Types in .NET
I know this is old question, but it's a basic question and I needed a detailed answer.
So I explored this and got results:
In .Net when you create an array (for this example I use int[]
) you take 6 bytes before any memory is allocated for your data.
Consider this code [In a 32 bit application!]:
int[] myArray = new int[0];
int[] myArray2 = new int[1];
char[] myArray3 = new char[0];
And look at the memory:
myArray: a8 1a 8f 70 00 00 00 00 00 00 00 00
myArray2: a8 1a 8f 70 01 00 00 00 00 00 00 00 00 00 00 00
myArray3: 50 06 8f 70 00 00 00 00 00 00 00 00
int[]
and char[]
(a8 1a 8f 70
vs 50 06 8f 70
)00 00 00 00
for myArray
and 01 00 00 00
for myArray2
00 00 00 00
). I don't know what its meaning.Now I feel a lot better about zero length array, I know how it works =]