C# - How to print all the values of an array with the Console.Writeline(??);command?

Asked

Viewed 2,358 times

0

//Copy an array and print the 2.

        int[] original = new int[] { 2, 5, 3, 8, 9, 12, 15, 7 };
        int[] copia = new int[original.Length];

        for (int index = 0; index < original.Length; index++)
        {
            copia[index] = original[index];


        }
        Console.WriteLine(); //o que eu colo para mostrar todos os valores de um dos arrays?
  • 2

    Console.WriteLine(String.Join(", ", copia));

1 answer

3


There are some options, here are some

int[] original = new int[] { 2, 5, 3, 8, 9, 12, 15, 7 };
int[] copia = new int[original.Length];

for (int index = 0; index < original.Length; index++) copia[index] = original[index];

Console.WriteLine("Original 1");
original.ToList().ForEach(p => Console.WriteLine(p.ToString()));

Console.WriteLine("Copia 1");
copia.ToList().ForEach(p => Console.WriteLine(p.ToString()));

Console.WriteLine("Original 2");
original.ToList().ForEach(Console.WriteLine);

Console.WriteLine("Copia 2");
copia.ToList().ForEach(Console.WriteLine);

Console.WriteLine("Original 3");
Console.WriteLine("[{0}]", string.Join(", ", original));

Console.WriteLine("Copia 3");
Console.WriteLine("[{0}]", string.Join(", ", copia));

Console.WriteLine("Original 4");
foreach (var item in original) Console.WriteLine(item.ToString());

Console.WriteLine("Copia 4");
foreach (var item in copia) Console.WriteLine(item.ToString());

Based in that reply of OS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.