Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:19:39+00:00 2026-05-10T17:19:39+00:00

I have some code where I’m returning an array of objects. Here’s a simplified

  • 0

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

Here’s a simplified example:

string[] GetTheStuff() {     List<string> s = null;     if( somePredicate() ) {         s = new List<string>(); // imagine we load some data or something     }     return (s == null) ?          new string[0] :         s.ToArray(); } 

The question is, how expensive is the new string[0] ?
Should I just return null and make the caller accept null as a valid way of indicating "nothing was found"?

NB: This is being called in a loop which gets run hundreds and hundreds of times, so it’s one of the few cases where I think this kind of optimiziation is not actually ‘premature’.

PS: And even if it was premature, I’d still like to know how it works 🙂

Update:

Initially when I asked if it used any space, I was thinking of things from the ‘C/C++’ point of view, kind of like how in C, writing char a[5]; will allocate 5 bytes of space on the stack, and char b[0]; will allocate 0 bytes.

I realise this is not a good fit for the .NET world, but I was curious if this was something that the compiler or CLR would detect and optimize out, as a non-resizeable array of size zero really shouldn’t (as far as I can see?) require any storage space.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:19:39+00:00Added an answer on May 10, 2026 at 5:19 pm

    Even if it’s being called ‘hundreds and hundreds’ of times, I’d say it’s a premature optimization. If the result is clearer as an empty array, use that.

    Now for the actual answer: yes, an empty array takes some memory. It has the normal object overhead (8 bytes on x86, I believe) and 4 bytes for the count. I don’t know whether there’s anything beyond that, but it’s not entirely free. (It is incredibly cheap though…)

    Fortunately, there’s an optimization you can make without compromising the API itself: have a ‘constant’ of an empty array. I’ve made another small change to make the code clearer, if you’ll permit…

    private static readonly string[] EmptyStringArray = new string[0];  string[] GetTheStuff() {     if( somePredicate() ) {         List<string> s = new List<string>();          // imagine we load some data or something         return s.ToArray();     } else {         return EmptyStringArray;     } } 

    If you find yourself needing this frequently, you could even create a generic class with a static member to return an empty array of the right type. The way .NET generics work makes this trivial:

    public static class Arrays<T> {     public static readonly Empty = new T[0]; } 

    (You could wrap it in a property, of course.)

    Then just use: Arrays<string>.Empty;

    EDIT: I’ve just remembered Eric Lippert’s post on arrays. Are you sure that an array is the most appropriate type to return?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, not really. When you tell your program to sleep,… May 11, 2026 at 9:11 pm
  • Editorial Team
    Editorial Team added an answer I don't believe you'll have any issue with the XSLT.… May 11, 2026 at 9:11 pm
  • Editorial Team
    Editorial Team added an answer My main thoughts on this are: Silverlight is cross browser… May 11, 2026 at 9:11 pm

Related Questions

I have some code where I'm returning an array of objects. Here's a simplified
I have some code where I am using a Linq-To-SQL DataContext to add and
I want to receive the following HTTP request in PHP: Content-type: multipart/form-data;boundary=main_boundary --main_boundary Content-type:
I have some code in MS VC++ 6.0 that I am debugging. For some
I have some code in a javascript file that needs to send queries back

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.