Assuming you have two collections / lists / arrays with the same number of elements, you can do:
// Example arrays
var billingInformation = new[] { 1, 2, 4 };
var shippingInformation = new[] { 1, 2, 4 };
for (int i = 0; i < billingInformation.Length && i < shippingInformation.Length; i++)
{
var billingItem = billingInformation[i];
var shippingItem = shippingInformation[i];
}
However, you can't simply do foreach
property in both classes. For that, you'd have to create a custom implementation and implement IEnumerable. In other words, you'd have something like this:
public class ShippingInformation : IEnumerable
{
public string Name { get; set; }
public string Address { get; set; }
public string Telephone { get; set; }
public IEnumerator GetEnumerator()
{
yield return Name;
yield return Address;
yield return Telephone;
}
}
Then you can use a foreach
for the object:
var shippingInformation = new ShippingInformation();
foreach (var data in shippingInformation)
{
// do something with your property which is of type object?
}
If you are sure that your properties that you want to be usable in a foreach
are always of the same type (e.g int
), in that case you can implement IEnumerable<int>
instead and yield return
your integer values inside the GetEnumerator
. For that I would suggest you to check out this question asked by another user: "How do I make a class iterable?".