Search code examples
c#arraysstring-interpolation

Interpolation C# in foreach loop, how to do that


I have following code in my programm:

        string[] strarray = new string[] {bird, lion, ape};
        Array.Reverse(strarray);

        strarray.Reverse();
        foreach(var value in strarray)
        {

           Console.Write(value.ToString());

        }

I want the Array "strarray" to be surrounded like that Array[ strarray ]. I cant use the string.Format here because it loops through it and with each string the text Array[ ... ] gets added. How should I do here?


Solution

  • Try this:

    string[] strarray = new string[] { "bird", "lion", "ape" };
    
    Console.Write($"Array[{String.Join(", ", strarray.Reverse())}]");
    

    That gives me:

    Array[ape, lion, bird]