I need to specify a type for IndvStaffReq
Since IndvLoc.StaffRequirements is a List<StaffRequirement> then the type of IndvStaffReq is StaffRequirement.
foreach (StaffRequirement IndvStaffReq in IndvLoc.StaffRequirements)
However it is generally just easier to use var and let the compiler figure this out. If you go this route then you can mouse over the variable in the IDE and see the type as well.
//Note that local variables should generally be camel cased to avoid conflicts with Pascal cased members
foreach (var IndvStaffReq in IndvLoc.StaffRequirements)
Any type works with foreach provided it either implements IEnumerable, IEnumerable<T> or provides the required methods of that interface. Whether the stored type is a value type (struct) or class is irrelevant.
But do note that value types (structs) use copy semantics for assignment and therefore you may be limited to what you do inside that foreach statement.
//This will copy the current struct into IndvStaffReq, for the life of this iteration you have 2 copies in memory
foreach (var IndvStaffReq in IndvLoc.StaffRequirements)
{
Console.WriteLine(IndvStaffReq.DBRef); //OK, reading doesn't matter that you have 2 copies
IndvStaffReq.EndTime = IndvStaffReq.StartTime.AddDays(1); //Compiles (probably), but wrong
};
Because value types use copy semantics the foreach copies the entire struct into your temp local variable. Any changes you try to make will alter the copied value, not the one in the list. The only way to modify the properties of a struct inside the list is to replace the existing instance actually stored in the list. You cannot do that with a foreach, or at least easily since you need the index
for (var index = 0; index < IndvLoc.StaffRequirements.Count; ++index)
{
//Get a value and make a change, but preference is for immutable structs
var current = IndvLoc.StaffRequirements[index];
current.EndTime = current.StartTime.AddDays(1);
//Replace the current value with an updated one
IndvLoc.StaffRequirements[index] = current;
}
This is how all value types work. In general value types (structs) should be immutable to ensure these kinds of changes are errors at compilation. Personally, in your scenario, I think a class makes more sense as the data isn't an atomic set of values (like a latlong or datetime) and therefore don't belong in a struct.