Surprisingly for me
new string[count];
is filled with null
s. So I came up with
var emptyStrings = Enumerable.Range(0, count)
.Select(a => String.Empty)
.ToArray();
which is very verbose. Isn't there a shorcut?
Create an array of empty strings that is the same size as an existing array. It is a common pattern to combine the previous two lines of code into a single line: str = strings(size(A)); You can use strings to preallocate the space required for a large string array.
Initialization of Arrays of Strings: Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword. However, Initializing an Array after the declaration, it must be initialized with the new keyword. It can't be initialized by only assigning values.
So in your code, you can use: private static final String[] EMPTY_ARRAY = new String[0];
To declare an empty array for a type variable, set the array's type to Type[] , e.g. const arr: Animal[] = [] . Any elements you add to the array need to conform to the specific type, otherwise you would get an error. Copied!
You can use Enumerable.Repeat
:
string[] strings = Enumerable.Repeat(string.Empty, count).ToArray();
(But be aware that creating a string array of the correct size and looping will give better performance.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With