Does an empty array in .NET use any space?

后端 未结 9 1535
暗喜
暗喜 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:25

    I would guess that an empty array uses only the space needed to allocate the object pointer itself.

    From memory the API guidelines say that you should always return an empty array from a method that returns an array rather than returning null, so I'd leave your code the way it is regardless. That way the caller knows he's guaranteed to get an array (even an empty one) and need not check for null with each call.

    Edit: A link about returning empty arrays:

    http://wesnerm.blogs.com/net_undocumented/2004/02/empty_arrays.html

提交回复
热议问题