简体   繁体   中英

Passing Arrays as Parameters in C#

I am trying to create a function that accepts 5 integers, holds them in an array and then passes that array back to the main. I have wrestled with it for a couple of hours and read some MSDN docs on it, but it hasn't "clicked". Can someone please shed some light as to what I am doing wrong? Errors: Not all code paths return a value; something about invalid arguments:

Code:

using System;
using System.IO;
using System.Text;

namespace ANumArrayAPP
{
class Program
{
    static void Main()
    {
        int[] i = new int[5];
        ReadInNum(i);

        for (int a = 0; a < 5; a++)
        {
            Console.Write(a);
        }
    }

    static int ReadInNum(int readIn)
    {
        int[] readInn = new int[5];

        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Please enter an interger");
            readInn[i] = Console.Read();
        }
    }
}

}

The parameter to ReadInNum is a single int , but you're trying to pass an array. You have to make the parameter match what you're trying to pass in. For example:

static void ReadInNum(int[] readIn)
{
    for (int i = 0; i < readIn.Length; i++)
    {
        Console.WriteLine("Please enter an interger");
        readIn[i] = int.Parse(Console.ReadLine());
    }
}

That will fill the array you pass into the method. Another alternative is to pass in how many integers you want, and return the array from the method:

static int[] ReadInNum(int count)
{
    int[] values = new int[count];
    for (int i = 0; i < count; i++)
    {
        Console.WriteLine("Please enter an interger");
        values[i] = int.Parse(Console.ReadLine());
    }
    return values;
}

You'd call this from Main like this:

int[] input = ReadInNum(5);
for (int a = 0; a < 5; a++)
{
    Console.Write(input[a]);
}

You should complete your function with return statement since its return type is not void. Also, you should change your return type to int[] since your return an array of ints, not just one int:

static int[] ReadInNum(int readIn)
{
    int[] readInn = new int[5];

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine("Please enter an interger");
        readInn[i] = Console.Read();
    }

    return readInn;
}

You should also change your main function in order to get the result of the function ReadInNum. It seems to me that you don't really use the parameter so you can remove it, if you don't have special attention:

static void Main()
{
    int j;
    int[] i = ReadInNum(j);

    for (int a = 0; a < 5; a++)
    {
        Console.Write(a);
    }
}

To summarize zome aswer already here, there are two ways to go about it:

1. Pass an array into the method

static void Main()
{
    int[] i = new int[5];
    ReadInNum(i);

    for (int a = 0; a < 5; a++)
    {
        Console.Write(i[a]);  // !
    }
}

static int ReadInNum(int[] readIn)
{
      for (int i = 0; i < rreadIn.Length; i++)
           ...
}

2. Return an array

static void Main()
{
    // int[] i = new int[5];
    int[] i = ReadInNum(5);

    for (int a = 0; a < i.Length; a++)
    {
        Console.Write(i[a]);  // !
    }
}

static int[] ReadInNum(int size)
{
      int[] readIn  = new int[size];

      for (int i = 0; i < readIn.Length; i++)
           ...
}

Pass an array to the function:

class Program
{
    static void Main()
    {
        int[] i = new int[5];
        ReadInNum(i);

        for (int a = 0; a < 5; a++)
        {
            Console.Write(a);
        }
    }

    static int[] ReadInNum(int[] readIn)
    {
        for (int i = 0; i < readIn.Length; i++)
        {
            Console.WriteLine("Please enter an interger");
            readInn[i] = Console.Read();
        }

        return readIn;
    }
}

Notice how I returned an int[] from ReadInNum. This isn't necessary but it makes the code more readable since it's not more obvious that you're planning on changing readIn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM