My initial intention is to send list asynchronously through a TCP Stream. But directly after passing the list to the asynchronous thread I need to clear it to fill it again with new data. So I used Shallow cloning to create a copy of the list, and pass it to the background thread:
private List<MyDataObject> GetShallowCloneOfDataList(List<MyDataObject> dataEvents)
{
return new List<MyDataObject>(dataEvents);
}
and here is my final code:
List<MyDataObject> data = new List<MyDataObject>();
while(hasMoreData)
{
data.clear();
FillListFromServer(data);
List<MyDataObject> clonedList = GetShallowCloneOfDataList(data);
Task.Run(() => SendDataList(clonedList));
}
My question is, when I clear the original list data
, will the items inside the cloned list be also affected? Testing my code revealed that they are not affected, but I am not sure if this will remain true when handling large amounts of data (200K per second).