Say I have a class or a struct defined as so:
public class foo
{
public int x;
public int y;
}
or
public struct bar
{
public int x;
public int y;
}
Say I have some array or list of objects, either an array of foo's
or bar's
. Now I want to create an array or list of the x's and/or y's. Is there a simple way to do this in C# that does not require iterating through every foo
or bar
of the array, and then adding the desired member variable to another array?
For example, this is how I am currently doing it:
//For some List<foo> fooList
//get the x's and store them to the array List<int> xList
foreach (var fooAtom in fooList)
{
xList.Add(fooAtom.x);
}
Is there a faster/less coding way of doing this? Is there a construct within C# designed specifically for this? Is it different for classes vs. structs? Thanks.