如果我有一个包含公共 int 属性(公共访问器)的对象,在实例化时初始化此属性时如何将字符串解析为 int ?
// 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 ?
});
编辑:我可以做到这一点,但想知道是否有办法直接在对象初始值设定项中做到这一点:
table.Rows.Select(row => {
int.TryParse(row["stringValue"], out int intProperty);
return new MyObject
{
IntProperty = intProperty;
}
});
一只甜甜圈
相关分类