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

0

// Make an array copy 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?
    
asked by anonymous 26.05.2018 / 14:40

1 answer

2

There are some options, some follow

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 on the SO

    
26.05.2018 / 14:52