EN
C# / .NET - create new array of strings
0
points
In this article, we would like to show you how to create new array of strings in C#.
Quick solution:
string[] strings1 = {"A", "B", "C"};
or:
string[] strings2 = new string[3];
strings2[0] = "A";
strings2[1] = "B";
strings2[2] = "C";
Practical examples
In this section, we present two ways of how to declare and initialize array of strings.
Example1
using System;
public class TestClass
{
public static void Main()
{
string[] strings1 = { "A", "B", "C" };
foreach (string str in strings1)
{
Console.WriteLine(str);
}
}
}
Output:
A
B
C
Example2
using System;
public class TestClass
{
public static void Main()
{
string[] strings2 = new string[3];
strings2[0] = "A";
strings2[1] = "B";
strings2[2] = "C";
foreach (string str in strings2)
{
Console.WriteLine(str);
}
}
}
Output:
A
B
C