【问题标题】:Using out parameter on class property [duplicate]在类属性上使用 out 参数[重复]
【发布时间】:2013-11-24 23:48:52
【问题描述】:

有没有办法在未初始化的对象属性上使用out

例如:

QuoteDetail q = new QuoteDetail();

Dictionary<int, string> messageDict = SplitMessage(msg);

messageDict.TryGetValue(8, out q.QuoteID); //doesn't work

【问题讨论】:

    标签: c# properties parameters parameter-passing out


    【解决方案1】:

    你必须启动 out 参数。

    以下来自 msdn 的链接应该会有所帮助...

    http://msdn.microsoft.com/en-us/library/ee332485.aspx

    【讨论】:

      【解决方案2】:

      简单回答:否

      您不能使用属性。您将不得不使用变量

      顺便说一句:已经回答了十几次: Passing a property as an 'out' parameter in C#

      【讨论】:

        【解决方案3】:

        不,你不能这样做。只需使用临时变量即可:

        QuoteDetail q = new QuoteDetail();
        
        Dictionary<int, string> messageDict = SplitMessage(msg);
        string quoteID;
        if (messageDict.TryGetValue(8, out quoteID))
        {
            q.QuoteID = quoteID;
        }
        

        【讨论】:

          猜你喜欢
          • 2013-03-16
          • 2013-01-05
          • 1970-01-01
          • 2014-10-01
          • 2018-05-18
          • 1970-01-01
          • 2010-11-25
          • 2010-10-15
          • 1970-01-01
          相关资源
          最近更新 更多