The simplest way to display an array is to use string.Join() to combine the elements of the array into one comma-separated string
int[] array = new int[] { 1,2,3,4,5 };
Console.WriteLine(string.Join(",", array));
with results
1,2,3,4,5
You can use Environment.NewLine instead of "," to place each value in a new line.
Also, note in your code you are using an array as an argument in your function, which fills in the array and then returns it. But you don't need to pass the array, just the desired size of the array
static int[] numbers(int count){
int[] values = new int[count];
for(int ind = 0; ind < 10; ind++){
Console.WriteLine("Type a number: ");
values[ind]= int.Parse(Console.ReadLine());
}
return values;
}
int[] array = numbers(10);
or fill in however many elements there are in the array already
static void numbers(ref int[] values){
for(int ind = 0; ind < 10; ind++){
Console.WriteLine("Type a number: ");
values[ind]= int.Parse(Console.ReadLine());
}
}
int[] array = new int[10];
numbers(ref array);
depending on how many times you are going to create a new array, or want to re-use an existing array over and over. The keyword ref is not required here because you can always modify the elements of an array, but it is added as a convention to indicate to the reader that the function numbers() indeed modifies the array argument.
Finally, I am going to explain line by line what is going on to demonstrate why the code isn't behaving as expected
numbers(new int[10]); this creates a new array of 10 elements and passes it the function numbers() the result of the function is never assigned to a local variable and thus lost forever.
for(int s= 1; s < 10; s++){ ... } this is a loop for 9 iterations (1 to 9).
Console.WriteLine(numbers(new int[s])); this creates a new array with s elements (again 1 to 9) and passes it to the function. The result of the function is passed to the WriteLine() method where the default behavior of arrays is to display the type and not the contents of the argument.
Possible fix to the above is to create the array once and access its elements in a loop (using the first update to numbers() from above)
int[] array = numbers(10);
for(int s= 0; s < array.Length; s++){
Console.WriteLine(array[s]);
}
Note that the iteration count is driven by array.Length instead of the hard-coded number 10.
from the array- there is no the array. There are two arrays, unrelated to each other, both of which get discarded after the call tonumbersreturns.