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.
2 Answers
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
}
Comments
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]);
}
foreachapproach?