This is quite common - especially when you are trying to make your code more data-driven - you need to iterate over related collections. For example, I just finished writing a piece of code that looks like this:
string[] entTypes = {"DOC", "CON", "BAL"}; string[] dateFields = {"DocDate", "ConUserDate", "BalDate"}; Debug.Assert(entTypes.Length == dateFields.Length); for (int i=0; i<entTypes.Length; i++) { string entType = entTypes[i]; string dateField = dateFields[i];
In Python, I would write something like:
items = [("DOC", "DocDate"), ("CON", "ConUserDate"), ("BAL", "BalDate")] for (entType, dateField) in items:
I do not need to declare parallel arrays, I do not need to state that my arrays are the same length, I do not need to use an index to display the elements.
I feel there is a way to do this in C # using LINQ, but I can't figure out what it could be. Is there any easy way to iterate over multiple related assemblies?
Edit:
This is a little better, I think - at least in the case where I have the luxury of manually buttoning collections when declaring and where all collections contain objects of the same type:
List<string[]> items = new List<string[]> { new [] {"DOC", "DocDate"}, new [] {"CON", "ConUserDate"}, new [] {"SCH", "SchDate"} }; foreach (string[] item in items) { Debug.Assert(item.Length == 2); string entType = item[0]; string dateField = item[1];