Shoaib Ahmed
Shoaib Ahmed

Reputation: 55

how to get values of object list using for loop in c#

I have a list of object type in c#. Now I want to get values of each element using for loop . I have searched a lot on web but I always found only foreach loop method.

Upvotes: 4

Views: 12854

Answers (2)

Alex
Alex

Reputation: 21766

You can loop over a list using for like shown below, although using foreach is generally cleaner

for (var i = 0; i < list.Count; i++) {
    var xcor = list[i].xcor;
    var ycor = list[i].ycor;
}

The equivalent foreach loop would look like this:

foreach (var point in list)
{
   var xcor = point.xcor;
   var ycor = point.ycor
}

Upvotes: 3

mrsargent
mrsargent

Reputation: 2442

This is how you would get each object in a list of objects with a for loop

List<object> list = new List<object>();

 for (int i = 0; i < list.Count; i++)
 {
      SomeMethodThatDoesSomethingWithAnObject(list[i]);
 }

Upvotes: 2

Related Questions