How to loop array of objects using forEach?

Yuriy

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.

edited at
0

Comments

0 comments
Login to comment

Related

how to access json array of objects using foreach loop in javascript and jade

How do i loop through these objects inside an array using forEach?

Loop through an array of objects using forEach

Using foreach-loop to construct objects in the array

How to get the array values using foreach loop

How to modify array value using foreach loop?

how to remove object from array of objects in foreach loop php

how to loop through this php associative array using foreach loop

Pushing objects into array using forEach

How Do I Pass on an Array of Objects using "forEach" Function?

PHP push objects to array from foreach loop

forEach loop in nested array of objects Javascript

How to encode json array inside foreach loop using Php

How do I loop through using foreach for a json decoded array

How to get key of multidimensional array in foreach loop using php?

How to use foreach loop using two array to get result

How to push data in empty space in array using foreach loop?

How to use foreach loop in an array object variable using javascript

how can I restructure an associative array using a PHP foreach loop (or not)?

How do I call an array within an IIFE using a forEach loop?

I want to get specific values inside multiple Objects in One array using forEach or a loop in PHP

Vue Js using forEach loop to compare array objects, prints out value multiple times

How to concatenate an array with a 'foreach' loop?

How to create array in foreach loop?

Array within array using foreach loop in codeigniter

How to assign objects to a Point array using a for each loop

How to loop over an array of objects and display it in UI using angular 8

How do I display array objects using a simple loop and 'if' statements?

How to loop an array of objects using Kusto Query Language