432

Is it possible to create an empty array without specifying the size?

For example, I created:

String[] a = new String[5];

Can we create the above string array without the size?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yogesh
  • 4,455
  • 2
  • 14
  • 13
  • 2
    http://stackoverflow.com/questions/151936/does-an-empty-array-in-net-use-any-space u check this link, it may help you to understand array things clearly – Ravi Gadag Jan 04 '12 at 12:51
  • 4
    Also see [all-possible-c-sharp-array-initialization-syntaxes](http://stackoverflow.com/questions/5678216/all-possible-c-sharp-array-initialization-syntaxes) – nawfal Sep 03 '13 at 15:30

15 Answers15

526

If you are going to use a collection that you don't know the size of in advance, there are better options than arrays.

Use a List<string> instead - it will allow you to add as many items as you need and if you need to return an array, call ToArray() on the variable.

var listOfStrings = new List<string>();

// do stuff...

string[] arrayOfStrings = listOfStrings.ToArray();

If you must create an empty array you can do this:

string[] emptyStringArray = new string[0]; 
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 3
    @Oded -- string[] emptyStringArray = new string[0]; does not result in an empty array, does it? It looks like it's an array with one element where that element is null. – rory.ap Sep 27 '13 at 14:24
  • 20
    @roryap - No. It results in a `string[]` that has no elements. If you try to access `emptyStringArray[0]`, you will get a `IndexOutOfRangeException` – Oded Sep 27 '13 at 14:25
  • 1
    @Oded -- Thanks, I'm fairly new to C#. In VB, the index provided is the upper bound, not the number of elements. – rory.ap Sep 27 '13 at 15:47
  • 1
    What would you consider better: var strings = new string[]{}; or var strings = new string[0]; BTW: I consider an empty array a perfectly valid default for method parameters: public void EditItems(IEnumerable toEdit, IEnumerable toDelete = new long[]{}) – realbart Aug 05 '14 at 07:58
  • For a practical need for the use of an empty array, see [this answer](http://stackoverflow.com/a/41289667/1497596) to the question: ["Create a REG_NONE empty value in the Windows Registry via C#."](http://stackoverflow.com/q/41289666/1497596) – DavidRR Dec 22 '16 at 18:40
  • 1
    You should use arrays rather than lists (ask Bjarne Stroustrup) and there is no defending the amount of boilerplate junk C# requires --which is why they continue to chip away at relieving users of doing what the language should be doing. – Rick O'Shea Feb 09 '17 at 21:35
  • 10
    @RickO'Shea - C++ is not C#. Stroustrup knows his C++ - not so sure he knows his C# and the .NET GC. Not gonna go into a religious war with you. – Oded Feb 09 '17 at 22:16
  • A list would be fast for insertion/deletion, but would be slower to index or iterate over, so it really depends on your needs. That being said, I'm sure Stroustrup is familiar with C#, he's quite an intelligent and upstanding person. I'm not quite sure why O'Shea brought him up here. – Nemo Jul 28 '21 at 19:00
  • @Nemo - are you sure about the indexing and iteration? Why not test it ([benchmarkdotnet](https://benchmarkdotnet.org/articles/overview.html) can help here)? – Oded Jul 29 '21 at 19:14
  • @realbart You can't use an empty array as a default method parameter as "default parameter values must be compile-time constants" - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1736?f1url=%3FappId%3Droslyn%26k%3Dk(CS1736) – Caltor Dec 05 '22 at 12:05
233

In .NET 4.6 the preferred way is to use a new method, Array.Empty:

String[] a = Array.Empty<string>();

The implementation is succinct, using how static members in generic classes behave in .Net:

public static T[] Empty<T>()
{
    return EmptyArray<T>.Value;
}

// Useful in number of places that return an empty byte array to avoid
// unnecessary memory allocation.
internal static class EmptyArray<T>
{
    public static readonly T[] Value = new T[0];
}

(code contract related code removed for clarity)

See also:

lindexi
  • 4,182
  • 3
  • 19
  • 65
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 3
    Definitely an improvement for readability from: `Enumerable.Empty().ToArray()` – DanielV Jan 30 '19 at 07:07
  • 1
    Even though this method is definitely preferred in most cases, it doesn't answer the original question. The OP wants to **create** an empty array. `Array.Empty()` does **not** create an array. It returns a reference to a pre-allocated array. – l33t Aug 26 '19 at 10:57
  • Why is `EmptyArray` a separate class instead of just having this be part of `Array`? – Aaron Franke Jul 25 '21 at 18:14
  • 1
    @AaronFranke this is an `internal` class anyway, and the source code linked includes a comment that states this done to avoid unnecessary memory allocation. To be very honest I not sure what use an empty array is, so if microsoft has deemed its more efficient to just throw you a reference to a pre-determined one, so the compiler doesn't have to waste its time, I don't see anything wrong with that. I would prefer Array.Empty() simply because it clearly communicates the intention; if I saw `T[0]` of anything my first inclination would be to ask if this was an code error – Narish Feb 08 '23 at 21:35
216

Try this:

string[] a = new string[] { };
lindexi
  • 4,182
  • 3
  • 19
  • 65
hemant
  • 2,405
  • 1
  • 13
  • 5
  • 7
    Expanding on this answer, you can also init the array with items without specifying the size OR type, the compiler will infer either from the initializer: var a = new []{"a", "b", "c"}; This is still a strongly typed string array. – Nick VanderPyle Jan 04 '12 at 14:12
  • 1
    Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun ds of the array. at ArrayClass.Main(String[] args). I faced this error after i changed my int[] variable = new int[]{} – yogesh Jan 05 '12 at 10:25
  • 8
    @yogesh: That is strange. When for example writing `int[] variable = new int[]{}` and using it for example in a loop such as `foreach (var s in variable){ Console.WriteLine(s);}` the code is compiled to: `int[] args1 = new int[0];` and `foreach (int num in args1){Console.WriteLine(num);}`. So there should be no difference between using `new int[0]` and `new int[]{}` as both get compiled to the same code. – Nope Dec 17 '12 at 12:15
  • 1
    @GlennGordon Absolutely, but that is new as of C# version 3.0 (from 2007, with Visual Studio 2008). That version also allows another simple format with `var`, although only for local variables (not for fields). However in C# 2.0 (Visual Studio 2005) and earlier, you had to use the syntax of this answer (or `string[] a = new string[0];`). – Jeppe Stig Nielsen Apr 18 '16 at 09:24
39

You could inititialize it with a size of 0, but you will have to reinitialize it, when you know what the size is, as you cannot append to the array.

string[] a = new string[0];
Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
MatthiasG
  • 4,434
  • 3
  • 27
  • 47
11

There is not much point in declaring an array without size. An array is about size. When you declare an array of specific size, you specify the fixed number of slots available in a collection that can hold things, and accordingly memory is allocated. To add something to it, you will need to anyway reinitialize the existing array (even if you're resizing the array, see this thread). One of the rare cases where you would want to initialise an empty array would be to pass array as an argument.

If you want to define a collection when you do not know what size it could be of possibly, array is not your choice, but something like a List<T> or similar.

That said, the only way to declare an array without specifying size is to have an empty array of size 0. hemant and Alex Dn provides two ways. Another simpler alternative is to just:

string[] a = { };

[The elements inside the bracket should be implicitly convertible to type defined, for instance, string[] a = { "a", "b" };]

Or yet another:

var a = Enumerable.Empty<string>().ToArray();

Here is a more declarative way:

public static class Array<T>
{
    public static T[] Empty()
    {
        return Empty(0);
    }

    public static T[] Empty(int size)
    {
        return new T[size];
    }
}

Now you can call:

var a = Array<string>.Empty();

//or

var a = Array<string>.Empty(5);
Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 1
    I cannot think of any use except when you got to pass an array as a parameter. There are a few cases in reflection where a method accepts an array of objects and you might want to pass an empty array to effect default action. I will edit my answer. – nawfal Apr 29 '15 at 09:04
  • You for example have an interface implemented by several classes returning an IEnumerable and one of the implementations do not have elements for the method and returns an empty array for example. – Ignacio Soler Garcia Apr 29 '15 at 09:33
  • @IgnacioSolerGarcia I would return an array in that case if and only if it's an extremely performance critical application. I will say arrays are outdated and should be avoided if you can. See [this by Lippert](http://blogs.msdn.com/b/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx) and [this S.O. question](http://stackoverflow.com/questions/75976/how-and-when-to-abandon-the-use-of-arrays-in-c) – nawfal Apr 29 '15 at 12:26
  • Have in mind that the returned type is IEnumerable so the array is read-only and it is not intended to change. Why would I return a List in this case for example? – Ignacio Soler Garcia Apr 29 '15 at 12:48
  • @IgnacioSolerGarcia One, `IEnumerable` can be cast back to its original type, ie, array. Two, array is not truly read-only, items in any slot can be replaced with another instance. Three, of course you shouldn't return a `List` back if readonly-ness matter, you should return a genuine readonly type collection in .NET, say for eg, `ReadOnlyCollection`. The cases where arrays logically make sense are very very rare. Kindly go through that Lippert's link, it explains it beautifully. – nawfal Apr 29 '15 at 12:53
  • I agree with what the the link explains about returning arrays as it really makes really sense. On the other way I think it would be extremely uncommon to cast an IEnumerable back to an array (even when this could be done). So all in all I think that returning an empty array in this case is safe and much more performant that returning an empty ReadOnlyCollection. Anyway I get your point, thanks. – Ignacio Soler Garcia Apr 29 '15 at 14:00
  • If u don't worry about casting then returning a list would do the job too as the client only gets to operate on ienumerable. The performance benefits hardly matters imo. Logically arrays rarely makes sense. Sortedset, dictionary, ordereddictionary, queues, list, readonlycollection, immutablelist etc all translate to one specific use case or the other. What is an array? An editable but not removable, indexable collection of fixed size? Makes sense? It's a specialised low level structure which has its own quirks when you dig deep, esp with reflection. See the so q i linked to see array caveats. – nawfal Apr 29 '15 at 14:20
  • 1
    A use case for an empty array is simple - when you want to fill it with objects and you don't know how many you'll be adding! – vapcguy May 08 '15 at 03:14
  • @vapcguy 'when u don't know the how many' - this is the case when u use linearly growing structures. Any empty structure can be used in place of an array there. Unless u r dealing with legacy code which demands array. – nawfal May 08 '15 at 06:58
  • This answer starts very well by explaining, that arrays must have a size. Unfortunately later it reinvents the wheel instead of using .NET `Array.Empty()`. – 0xF Sep 17 '21 at 17:13
  • @0xF Fortunately `Array.Empty()` wheel had not been invented when I wrote this answer :) – nawfal Sep 23 '21 at 09:12
  • 1
    @nawfal `Array.Empty()` appeared in .NET Framework 4.6, so one year after your answer. Apologies! – 0xF Sep 23 '21 at 11:04
10

string[] a = new string[0];

or short notation:

string[] a = { };

The preferred way now is:

var a = Array.Empty<string>();

I have written a short regular expression that you can use in Visual Studio if you want to replace zero-length allocations e.g. new string[0]. Use Find (search) in Visual Studio with Regular Expression option turned on:

new[ ][a-zA-Z0-9]+\[0\]

Now Find All or F3 (Find Next) and replace all with Array.Empty<…>() !

juFo
  • 17,849
  • 10
  • 105
  • 142
9

Simple and elegant!

string[] array = {}
disklosr
  • 1,536
  • 17
  • 26
  • I would change `array` to just `a`, as `array` is a keyword when capitalized. Just bad practice to use a keyword name as a variable name - even if the case is different. And basically the same as my answer except I had `String.Empty` in there. – vapcguy Jun 13 '19 at 13:31
  • 4
    1. array is not a c# keyword. Array is a class and not a keyword 2. "a" is also bad practice (maybe even worse a bad practice than using keywords) – disklosr Jun 14 '19 at 14:29
  • Being technical. Class, keyword, it defines an object type and is still bad. Why do you think `a` is bad? – vapcguy Jun 14 '19 at 18:17
  • 1
    Because non-descriptive and one-letter variable names are bad practice because they don't convey the reason behind defining them. "array" is certainly a better name than "a". A better name would be "emptyArray". – disklosr Jun 15 '19 at 21:41
7

Performance Rule CA1825: Avoid allocating zero-length arrays.

Rule discription: Initializing a zero-length array leads to an unnecessary memory allocation. Instead, use the statically allocated empty array instance by calling the Array.Empty method.

In your case:

var a = Array.Empty<string>(); 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Igor Nedbaylo
  • 101
  • 1
  • 3
4

You can define array size at runtime.

This will allow you to do whatever to dynamically compute the array's size. But, once defined the size is immutable.

Array a = Array.CreateInstance(typeof(string), 5);
radarbob
  • 4,964
  • 2
  • 23
  • 36
3

I had tried:

string[] sample = new string[0];

But I could only insert one string into it, and then I got an exceptionOutOfBound error, so I just simply put a size for it, like

string[] sample = new string[100];

Or another way that work for me:

List<string> sample = new List<string>();

Assigning Value for list:

sample.Add(your input);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CS_YAN
  • 41
  • 5
  • If you want to add to an array, you can use ar.Append(item) and assign it back to itself (creating a new array and replacing the old one) ar = ar.Append(item); – spacebread Apr 06 '23 at 15:55
2

As I know you can't make array without size, but you can use

List<string> l = new List<string>() 

and then l.ToArray().

nawfal
  • 70,104
  • 56
  • 326
  • 368
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
1

Combining @nawfal & @Kobi suggestions:

namespace Extensions
{
    /// <summary> Useful in number of places that return an empty byte array to avoid unnecessary memory allocation. </summary>
    public static class Array<T>
    {
        public static readonly T[] Empty = new T[0];
    }
}

Usage example:

Array<string>.Empty

UPDATE 2019-05-14

(credits to @Jaider ty)

Better use .Net API:

public static T[] Empty<T> ();

https://learn.microsoft.com/en-us/dotnet/api/system.array.empty?view=netframework-4.8

Applies to:

.NET Core: 3.0 Preview 5 2.2 2.1 2.0 1.1 1.0

.NET Framework: 4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6

.NET Standard: 2.1 Preview 2.0 1.6 1.5 1.4 1.3

...

HTH

ShloEmi
  • 1,851
  • 2
  • 20
  • 25
1

you can use the Array.Empty method (in .Net Core, at least)

string ToCsv(int[] myArr = null) { // null by default

    // affect an empty array if the myArr is null
    myArr ??= Array.Empty<int>();
    
    //... do stuff
    string csv = string.Join(",", myArr);

    return csv;
}
serge
  • 13,940
  • 35
  • 121
  • 205
0

Here is a real world example. In this it is necessary to initialize the array foundFiles first to zero length.

(As emphasized in other answers: This initializes not an element and especially not an element with index zero because that would mean the array had length 1. The array has zero length after this line!).

If the part = string[0] is omitted, there is a compiler error!

This is because of the catch block without rethrow. The C# compiler recognizes the code path, that the function Directory.GetFiles() can throw an Exception, so that the array could be uninitialized.

Before anyone says, not rethrowing the exception would be bad error handling: This is not true. Error handling has to fit the requirements.

In this case it is assumed that the program should continue in case of a directory which cannot be read, and not break- the best example is a function traversing through a directory structure. Here the error handling is just logging it. Of course this could be done better, e.g. collecting all directories with failed GetFiles(Dir) calls in a list, but this will lead too far here.

It is enough to state that avoiding throw is a valid scenario, and so the array has to be initialized to length zero. It would be enough to do this in the catch block, but this would be bad style.

The call to GetFiles(Dir) resizes the array.

string[] foundFiles= new string[0];
string dir = @"c:\";
try
{
    foundFiles = Directory.GetFiles(dir);  // Remark; Array is resized from length zero
}
// Please add appropriate Exception handling yourself
catch (IOException)
{
  Console.WriteLine("Log: Warning! IOException while reading directory: " + dir);
  // throw; // This would throw Exception to caller and avoid compiler error
}

foreach (string filename in foundFiles)
    Console.WriteLine("Filename: " + filename);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Philm
  • 3,448
  • 1
  • 29
  • 28
-1

You can do:

string[] a = { String.Empty };

Note: OP meant not having to specify a size, not make an array sizeless

As if string[] a = {}; and the above really matter as far as their differences! The point to doing either one is only going to be instantiation or clearing the array before (re-)populating it.

vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • 9
    Won't this create a string array of length 1? – Sumner Evans May 08 '15 at 04:05
  • True, but it's cleared. And OP asked to declare the array without having to state a size - this fits that. – vapcguy May 08 '15 at 04:13
  • And that deserves a downvote, why? OP meant not having to `specify` a size, not make an array `sizeless`. – vapcguy May 09 '15 at 02:03
  • I did not downvote. If you add some more explanation to your answer, you might get some upvotes (or at least less get un-downvoted). – Sumner Evans May 09 '15 at 03:23
  • 1
    @vapcguy I was the downvoter. I regret it. I have edited your answer to cancel my downvote. Your comment makes the question a bit dubious. Not sure if that is what OP meant. – nawfal May 12 '15 at 11:30
  • @nawfal Thanks for the edit and downvote cancel. It's how I read what the OP posted, anyway. I actually use this method a lot, ex. when doing a `.Split()` on a semi-colon delimited string I'll set it equal to `a` so that `a` will be available at the level where I first instantiated it, usually at the top of the function, instead of having to use the array only where I do the `.Split()` if I had to set it equal to a `string[]` variable right there, like buried within some `if...then` or loop within that. – vapcguy May 20 '15 at 02:08
  • 5
    The OP asked for an **empty** array. This array is not empty. – Keith May 07 '19 at 15:30
  • @Keith It's an array of one object that is an empty string. I think you're playing technicalities. There's an answer that was done after mine, here, that has `string[] array = {}` - perhaps that's better for you - though I wouldn't use `array` as a variable name since it's a keyword when capitalized. – vapcguy Jun 13 '19 at 13:30