Languages
[Edit]
EN

C# / .NET - create new array of strings

0 points
Created by:
Nabila-Burnett
385

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join