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?
-
2Why do you need an empty array? What are you trying to do?Mr T.– Mr T.2013年05月30日 10:52:56 +00:00Commented May 30, 2013 at 10:52
-
12@MrT. not really relevant to the question.James– James2013年05月30日 10:56:13 +00:00Commented May 30, 2013 at 10:56
-
1@James - maybe there is a more elegant way to do whatever he is trying to do.Mr T.– Mr T.2013年05月30日 10:59:56 +00:00Commented May 30, 2013 at 10:59
-
4@aquanat - although an answer has already been accepted and i don't know your code or the logic behind it, i would humbly suggest you not to return an empty array but null and check from the calling function if the returned value is null or not. I think that it would be much more elegant, readable and efficient.Mr T.– Mr T.2013年05月30日 12:07:34 +00:00Commented May 30, 2013 at 12:07
-
7The reason you would not want to return null is that the consumer of the method has to check for null. For instance, the consumer of the method could put the returned value in a foreach and if an empty array is returned, there is no problem. However, if null is returned, there has to be a check for null before iterating the returned valueJAB– JAB2014年11月18日 17:56:31 +00:00Commented Nov 18, 2014 at 17:56
11 Answers 11
Try this
string[] arr = new string[] {};
3 Comments
c# Array.Empty<string>()Your syntax is wrong:
string[] arr = new string[]{};
or
string[] arr = new string[0];
2 Comments
If you are using .NET Framework 4.6 and later, they have some new syntax you can use:
using System; // To pick up definition of the Array class.
var myArray = Array.Empty<string>();
1 Comment
You can try this
string[] arr = {};
1 Comment
null for me when using within an object initializer.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)
Comments
Your syntax is invalid.
string[] arr = new string[5];
That will create arr, a referenced array of strings, where all elements of this array are null. (Since strings are reference types)
This array contains the elements from arr[0] to arr[4]. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to null.
1 Comment
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();
1 Comment
Those curly things are sometimes hard to remember, that's why there's excellent documentation:
// Declare a single-dimensional array
int[] array1 = new int[5];
1 Comment
int with string if he likes.// zero-element array
var arr0 = System.Array.Empty<string>(); // Recommended: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1825
var arr1 = new string[0];
// 5-Elements array with null values
var arr2 = new string[5];
// 5-Elements array with empty string values
var arr3 = System.Linq.Enumerable.Repeat(string.Empty, 5).ToArray();
var arr4 = System.Linq.Enumerable.Range(0, 5).Select(_ => string.Empty).ToArray();
Comments
could get something like this with: private List list = new List(); string[] dnormlines kist.ToArray();
Comments
The following should work fine.
string[] arr = new string[] {""};