Checking for null before ToString()

前端 未结 12 1522
暖寄归人
暖寄归人 2020-12-01 02:21

Here\'s the scenario...

if (entry.Properties[\"something\"].Value != null)
  attribs.something = entry.Properties[\"something\"].Value.ToString();
<         


        
12条回答
  •  悲哀的现实
    2020-12-01 03:09

    To do precisely what you're trying to do a helper method can always be used:

    CopyIfNotNull(entry.Properties["something"].Value, out attribs.something);
    
    void CopyIfNotNull(string src, out string dest)
    {
      if(src != null)
        dest = src;
    }
    

提交回复
热议问题