Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 83k

how to return an array from a method?

Feb 9 2022 5:37 PM

Hi
I want to pass an array to a method which must multiply all the numbers of the array by 2 and return the new array to Main. 
This gives as output: System.Int32[] 5 times.
Thanks 

static int[] mm(int[] arr1) 
    {
        for (int i = 0; i < arr1.Length; i++)
            arr1[i] *=2;
        return arr1;
    }
    static void Main()
    {
        int[] arr1 = new int[5];

        Console.Write("Input the number:");
        for (int j = 0; j < 5; j++)
        {
            Console.Write("number - {0} : ", j + 1);
            arr1[j] = Convert.ToInt32(Console.ReadLine());
        }

        for (int j = 0; j < 5; j++)
            Console.Write(mm(arr1) + " ");
    }


Answers (2)