This appeared in a code review, and there was some disagreement on what to do. It includes a function that converts one type of array of objects to another.
The complaint was that you double the memory used for a short period of time, because the function is called many times. And at some point, both the temp observablecollection and observablecollection parameters that are assigned to the temp observablecollection variable will have data. So, right before the task is completed, the program memory will spike. There were also concerns that garbage collection would not be fast enough.
Many solutions have been proposed, but all of them are associated with a violation of the encapsulation of the method and a decrease in the simplicity of maintainability. Can anyone suggest an answer for this?
Edit: I see that many people point out that this is premature optimization. It also appeared in a code review. I think that for this example we could assume that this list would consume a huge amount of memory. The review boiled down to memory savings and reliable code maintenance. So, I think the real question is is there a solution that will make both camps happy?
Dramatically simplified code below:
public class ClassExample { public ObservableCollection<OriginalObjectType> OriginalObjectTypes { get; set; } public void ConvertNewObjectTypesToOriginal(ObservableCollection<ObjectType2> objectType2s) { ObservableCollection<OriginalObjectType> originalObjectTypesTemp = new ObservableCollection<OriginalObjectType>(); foreach (var objectType2 in objectType2s) { var originalObjectType = new OriginalObjectType { Value = objectType2.Value }; originalObjectTypesTemp.Add(originalObjectType); } this.OriginalObjectTypes = originalObjectTypesTemp; } }
source share