0Day Forums
C# declare empty string array - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: C# (https://0day.red/Forum-C)
+--- Thread: C# declare empty string array (/Thread-C-declare-empty-string-array)



C# declare empty string array - pyloric227285 - 07-24-2023

I need to declare an empty string array and i'm using this code

string[] arr = new String[0]();

But I get "method name expected" error.

What's wrong?


RE: C# declare empty string array - sicklemia516184 - 07-24-2023

You can try this

string[] arr = {};


RE: C# declare empty string array - lennartvrknj - 07-24-2023

Arrays' constructors are different. Here are some ways to make an empty string array:

var arr = new string[0];
var arr = new string[]{};
var arr = Enumerable.Empty<string>().ToArray()

(sorry, on mobile)


RE: C# declare empty string array - animadvert243 - 07-24-2023

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

string[] arr = new string[0];

If you don't know about the size then You may also use `List<string>` as well like

var valStrings = new List<string>();

// do stuff...

string[] arrStrings = valStrings.ToArray();


RE: C# declare empty string array - trevontvmch - 07-24-2023

Your syntax is wrong:

string[] arr = new string[]{};

or

string[] arr = new string[0];



RE: C# declare empty string array - shoeblack924 - 07-24-2023

Those curly things are sometimes hard to remember, that's why there's excellent [documentation](

[To see links please register here]

):

// Declare a single-dimensional array
int[] array1 = new int[5];