If I have an object containing a public int property (public accessors), how can I parse a string to int when initializing this property at instantiation ?
// Given initialized DataTable table;
// Given public int IntProperty {get; set;} in public class MyObject
table.Rows.Select(row => new MyObject
{
int.TryParse(row["stringValue"], IntProperty), // MyObject.IntProperty is unknown here
IntProperty = int.TryParse(row["stringValue"], ... ) // IntProperty is known but what about the out int result argument of Int32.TryParse ?
});
EDIT : I could do this but want to know if there is a way to do it directly inside object initializer :
table.Rows.Select(row => {
int.TryParse(row["stringValue"], out int intProperty);
return new MyObject
{
IntProperty = intProperty;
}
});