In the code below, I am asking the user to give the dimensions of a multidimensional array.
The user is then told to enter the values for the array.
I then want to display the contents of an array by passing it to a method called display.
The question I have is that how can I make this method accept an array of any data type?
Say if I create an array that holds string data types and I want to display its contents, then I don't want to create a separate method for this. I want to be able to still pass it to the same method.
Thanks!
The user is then told to enter the values for the array.
I then want to display the contents of an array by passing it to a method called display.
The question I have is that how can I make this method accept an array of any data type?
Say if I create an array that holds string data types and I want to display its contents, then I don't want to create a separate method for this. I want to be able to still pass it to the same method.
Thanks!
C#:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Enter the number of rows: ");
int rows = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the number of columns: ");
int cols = int.Parse(Console.ReadLine());
int[,] matrix = new int[rows,cols];
Console.WriteLine("Enter the values for your matrix");
for(int x=0; x<rows; x++)
{
for(int y=0; y<cols; y++)
{
matrix[x,y] = int.Parse(Console.ReadLine());
}
}
Program.display(matrix);
}
public static void display([COLOR=#00ff00]??[/COLOR] [,] arr) //what datatype to use here for the array if this method can accept string,float,int etc?
{
for(int x=0; x<arr.GetLength(0); x++)
{
for(int y=0; y<arr.GetLength(1); y++)
{
Console.Write(arr[x,y] + " ");
}
Console.WriteLine();
}
}
}