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
try it
string[] arr = new string[] {};
Your syntax is incorrect:
string[] arr = new string[]{};
or
string[] arr = new string[0];
You can try this
string[] arr = {};
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)
If you are using .net 4.6, they have a new syntax that you can use:
var myArray = Array.Empty<string>();
If you must create an empty array, you can do this:
If you don't know about size, you can also use List<string> as well
List<string>
var valStrings = new List<string>(); // do stuff... string[] arrStrings = valStrings.ToArray();
Those curly things are sometimes hard to remember, so great documentation :
// Declare a single-dimensional array int[] array1 = new int[5];
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)
string
arr
null
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 .
arr[0]
arr[4]
new
Single-Dimensional Arrays (C# Programming Guide)
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 }