第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

匿名實(shí)例化期間對(duì)象初始值設(shè)定項(xiàng)中的輸出參數(shù)

匿名實(shí)例化期間對(duì)象初始值設(shè)定項(xiàng)中的輸出參數(shù)

C#
動(dòng)漫人物 2021-10-09 16:42:14
如果我有一個(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;    }});
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

我非常同意 Jeroen Mostert。與其“將所有內(nèi)容都?jí)嚎s到對(duì)象初始值設(shè)定項(xiàng)中”,不如讓您的代碼可讀且易于理解。比它可能編譯沒有問題:


var result = new List<MyObject>();

foreach(var row in table.Rows)

{

    var instance = new MyObject();

    int value;

    if(int.TryParse(row["stringValue"], out value)

        instance.IntProperty = value;

    result.Add(instance);

}

在 C#7 中,您還可以將其簡化為以下內(nèi)容:


var instance = new MyObject();

if(int.TryParse(row["stringValue"], out int value)

    instance.IntProperty = value;

result.Add(instance);


查看完整回答
反對(duì) 回復(fù) 2021-10-09
  • 3 回答
  • 0 關(guān)注
  • 243 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)