In the below shown examples, the former approach instantiates an object of type SearchResult
for each iteration whereas the latter one instantiates the object only once and uses it for all the iterations.
using (DbDataReader reader = (DbDataReader)dbObject.ExecuteReader(command))
{
if (reader.HasRows)
{
List<SearchResult> searchResults = new List<SearchResult>();
while (reader.Read())
{
SearchResult searchResult = new SearchResult();
searchResult.AppName = Convert.ToString(reader["AppName"]);
searchResults.Add(searchResult);
}
}
}
OR
using (DbDataReader reader = (DbDataReader)dbObject.ExecuteReader(command))
{
if (reader.HasRows)
{
List<SearchResult> searchResults = new List<SearchResult>();
SearchResult searchResult = new SearchResult();
while (reader.Read())
{
searchResult.AppName = Convert.ToString(reader["AppName"]);
searchResults.Add(searchResult);
}
}
}
Which is the better approach to handle this?
Thanks and kindly ignore my naiveness. :)