对象字符串Coalesce

时间:2013-11-11 13:32:08

标签: c# visual-studio-2010 sharepoint-2010

美好的一天,

我试图在SPListItem中使用coalesce。我想要完成的事情很简单。检查对象是否有值,如果为true则采用SPListItem String并将其放入变量中,如果为false则将string.Empty放入变量中。

虽然Coalesce无法做到这一点。我可以创建一个接受SPListItem的扩展方法并执行此操作吗? 到目前为止我有这个:

private static string Coalesce(Object historyListItem)
{
    try
    {
        return historyListItem.ToString();
    }
    catch (Exception ex)
    {
        return string.Empty;
    }
}

我认为这不是正确的做法。关于我如何修改的任何想法是遵守“微软最佳实践”?

1 个答案:

答案 0 :(得分:0)

如果问题为空,您可以执行以下操作:

return historyListItem == null ? string.Empty : historyListItem.ToString();
相关问题