I have some code that creates objects in a foreach loop. So:
private IEnumerable<StrongTypeResult> ConvertResults(List<Object> results)
{
return results.Select(result => new StrongTypeResult(result)).ToList();
}
FYI, StrongTypeResult is a struct. Since each new StrongTypeResult(result) object is allocated on the stack, if there are a large amount of objects to create, am I going to run into a stackoverflow issue? Would it be better to make StrongTypeResult a class?
First, you don’t need a foreach here. Select already returns an IEnumerable of the desired type. You don’t need a ToList also. Transforming delegate will be called when foreaching the IEnumerable on the client.
Structs are created on stack when you assign a struct to a variable. If you store them in a list or in an array,they will be on heap.
Please learn about differences between reference types, and value types, such as structs.
Generally, structs are copied on assigning to another variable.