I want to display the fields of objects using forEach, but something is not working for me.It doesn’t show me the values ??that I entered, but only the type of Object
Transportation[] companys = new Transportation[2];
for (int i = 0; i <=companys.Length-1; i++)
{
companys[i] = new Transportation();
Console.WriteLine("Enter company: ");
companys[i].Name = Console.ReadLine();
Console.Write("Enter unit price: ");
companys[i].Cost = Convert.ToDouble(Console.ReadLine());
Console.Write("Weight: ");
companys[i].Weight = Convert.ToDouble(Console.ReadLine());
}
Console.Clear();
foreach (Transportation key in companys)// here is problem
{
Console.WriteLine("List "+companys[key]);
}
This will help.
foreach (Transportation key in companys)
{
Console.WriteLine("List "+ key.X);
}
where X is a string property of your Transportation class.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments