You may have noticed that, in multi-dimensional arrays, the elements of each dimension must be the same data type. Bear in mind that we have already come across a way of storing more than type of information with a single identifier. Reread the section on Structures to remind yourself of the basics.
The following program creates an array of structures to store the details of products for sale.
static TProduct[] products = new TProduct[4];
public struct TProduct
{
public string Description;
public double Price;
}
static void Main(string[] args)
{
for (int i = 0;i<products.Length;i++)
{
Console.Write("Enter the description for product {0}: ", i);
products[i].Description = Console.ReadLine();
Console.Write("Enter the price for product {0}: ", i);
products[i].Price = System.Convert.ToDouble(Console.ReadLine());
}
Console.Write("Enter the number of the product to search: ");
int prod = System.Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Product {0}, Description: {1}, Price: £{2}", prod, products[prod].Description, products[prod].Price);
Console.ReadLine();
}