Stack User Stack User - 28 days ago 5
C# Question

how to assign empty to string array in c#

How can I assign empty to string array in c#?

string [] stack; //string array


how to assign

stack = ""; //this statement gives error cannot implicitly convert to type string to string []

Answer

You cannot use:

string[] stack = "";

Since stack in here is an array of string. If you want to initialize empty string for each elements in array, you can use LINQ with Enumerable.Range to get the result, assume there are 10 items in here:

 string[] stack = Enumerable.Range(0, 10)
                            .Select(i => string.Empty)
                            .ToArray();
Comments