I have a loop where i create some string value based on certain conditions. I did place StringBuilder object outside of the loop and each time i have new row in loop i need to clear StringBuilder appended values for this row.
How do i clear those?
StringBuilder sb = new StringBuilder();
foreach (DataRow row in recipientsList.Rows)
{
sb.Length = 0;
sb.Append("<ul>");
if (row["needsToActivate"] == "1")
{
sb.AppendFormat("<li>{0}</li>", getUsersWithoutActivationTemplate());
}
if (row["needsToEnterSite"] == "1")
{
sb.AppendFormat("<li>{0}</li>", getUsersWithoutEnteringWebsiteForTwoWeeksTemplate());
}
if (row["needsPicture"] == "1")
{
sb.AppendFormat("<li>{0}</li>", getUsersWithoutPicturesTemplate());
}
if (row["needsText"] == "1")
{
sb.AppendFormat("<li>{0}</li>", getUsersWithoutTextTemplate());
}
if (row["needsCharacteristic"] == "1")
{
sb.AppendFormat("<li>{0}</li>", getUsersWithoutCharateristicsTemplate());
}
if (row["needsHobby"] == "1")
{
sb.AppendFormat("<li>{0}</li>", getUsersWithoutHobbiesTemplate());
}
sb.Append("</ul>");
}
Code with accepted answer;