I am currently reorganizing the code to replace Convert.To with TryParse.
I met the following bit of code that creates and assigns a property to an object.
List<Person> list = new List<Person>();
foreach (DataRow row in dt.Rows)
{
var p = new Person{ RecordID = Convert.ToInt32(row["ContactID"]) };
list.Add(p);
}
What I came up with for replacement:
var p = new Person { RecordID = Int32.TryParse(row["ContactID"].ToString(), out RecordID) ? RecordID : RecordID };
Any thoughts, opinions, alternatives to what I did?
source
share