Checking for null before ToString()

前端 未结 12 1495
暖寄归人
暖寄归人 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:08

    In C# 6.0 you can do it in a very elegant way:

    attribs.something = entry.Properties["something"].Value?.ToString();

    And here is an article about new null-conditional operator.

提交回复
热议问题