如果我有一個(gè)包含公共 int 屬性(公共訪問器)的對(duì)象,在實(shí)例化時(shí)初始化此屬性時(shí)如何將字符串解析為 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 ?});編輯:我可以做到這一點(diǎn),但想知道是否有辦法直接在對(duì)象初始值設(shè)定項(xiàng)中做到這一點(diǎn):table.Rows.Select(row => { int.TryParse(row["stringValue"], out int intProperty); return new MyObject { IntProperty = intProperty; }});
匿名實(shí)例化期間對(duì)象初始值設(shè)定項(xiàng)中的輸出參數(shù)
動(dòng)漫人物
2021-10-09 16:42:14