Does an empty array in .NET use any space?

后端 未结 9 1462
暗喜
暗喜 2021-01-01 10:39

I have some code where I\'m returning an array of objects.

Here\'s a simplified example:

string[] GetTheStuff() {
    List s = null;
             


        
相关标签:
9条回答
  • 2021-01-01 11:13

    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.

    0 讨论(0)
  • 2021-01-01 11:15

    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).

    0 讨论(0)
  • 2021-01-01 11:16

    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>();
    }
    
    0 讨论(0)
  • 2021-01-01 11:19

    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);
    
    0 讨论(0)
  • 2021-01-01 11:20

    Declared arrays will always have to contain the following information:

    • Rank (number of dimensions)
    • Type to be contained
    • Length of each dimension

    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

    0 讨论(0)
  • 2021-01-01 11:23

    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
    

    Lets explain that memory:

    • Looks like the first 2 bytes are some kind of metadata, as you can see it changes between int[] and char[] (a8 1a 8f 70 vs 50 06 8f 70)
    • Then it holds the size of the array in integer variable (little endian). so it's 00 00 00 00 for myArray and 01 00 00 00 for myArray2
    • Now it's our precious Data [I tested with Immediate Window]
    • After that we see a constant (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 =]

    0 讨论(0)
提交回复
热议问题