C # declares an empty array of strings

I need to declare an empty array of strings and I use this code

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

but I get the error "method name".

What's wrong?

thank

+82
string arrays c #
May 30 '13 at 10:52
source share
9 answers

try it

 string[] arr = new string[] {}; 
+136
May 30 '13 at 10:59
source share

Your syntax is incorrect:

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

or

 string[] arr = new string[0]; 
+53
May 30 '13 at 10:52
source share

You can try this

 string[] arr = {}; 
+6
May 30 '13 at 10:55
source share

Array constructors are different. Here are some ways to make an empty array of strings:

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

(sorry on mobile)

+6
May 30 '13 at 10:56
source share

If you are using .net 4.6, they have a new syntax that you can use:

 var myArray = Array.Empty<string>(); 
+2
Nov 01 '16 at 19:48
source share

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

 string[] arr = new string[0]; 

If you don't know about size, you can also use List<string> as well

 var valStrings = new List<string>(); // do stuff... string[] arrStrings = valStrings.ToArray(); 
+1
May 30 '13 at 10:53
source share

Those curly things are sometimes hard to remember, so great documentation :

 // Declare a single-dimensional array int[] array1 = new int[5]; 
+1
May 30 '13 at 10:54
source share

The syntax is invalid.

 string[] arr = new string[5]; 

This will create a string array referenced by arr , and all elements of the array are null . (Since strings are reference types)

This array contains elements from arr[0] to arr[4] . The new operator is used to create an array and initialize the elements of the array by default. In this example, all elements of the array are initialized to null .

Single-Dimensional Arrays (C# Programming Guide)

0
May 30 '13 at 10:53
source share
 Random random = new Random(); int randomNumber1 = random.Next(0, 100); int randomNumber2 = random.Next(0, 100); double som = randomNumber1 + randomNumber2; Console.WriteLine(randomNumber1); Console.WriteLine(randomNumber2); Console.WriteLine("Tel de getallen op"); bool juist_geantwoord = false; while (!juist_geantwoord) { string input = Console.ReadLine(); double input_omgezet; if (Double.TryParse(input, out input_omgezet)) //Double.Tryparse = no crach if (Convert.ToDouble(input_omgezet) == som) { Console.WriteLine("Juist!"); juist_geantwoord = true; } else { Console.WriteLine("Fout, probeer opnieuw"); juist_geantwoord = false; //moet niet perse } 
-2
Dec 14 '17 at 18:32
source share



All Articles