foreach (string sn in MACOrSerial.Split(','))
{
MACOrSerial = sn.Trim();
}
MACORSerial contains a string of text (EX. AA123241, BB123431, CC1231243) separated by commas.
I grab one substring and place that into the same MACORSerial.
This will not cause me any problems as the the foreach still will be using the original MACOrSerial in memory.
While I think this is the most memory efficient approach, is it proper or should I just make another string with a new name such as
MacORSerialSubString = sn.Trim()?
I am not having any memory issues. I just want to make sure my code is clean and concise.
Your assumption is incorrect – the loop goes over the
string[]resulting from theSplit– these are all new string instances.You are not saving any memory by reassigning a string to the original variable and you are losing readability by reuse of a variable.
Here is one approach that is more readable and uses some of the built in capabilities: