所以我得到了这段代码:
string BizID = DT1["Business_ID"].ToString();
if (string.IsNullOrEmpty(BizID))
{
}
else
{
DDLBustype.SelectedValue = BizID;
}
似乎应该有一种更简单的方法来做到这一点。也许我只是有一个大脑f * rt但是不应该有办法这样做而不必使用"否则"?
答案 0 :(得分:9)
将评论中的答案添加为社区维基,以便可以关闭该问题。
string BizID = DT1["Business_ID"]?.ToString(); // add null conditional operator in case it is null.
if (!string.IsNullOrEmpty(BizID))
{
DDLBustype.SelectedValue = BizID;
}
答案 1 :(得分:0)
我通常将此隐藏在 string 的扩展方法中,将其称为HasValue(),因此它与Nullable类型具有的类似。然后我的代码看起来更干净,如:
if(firstName.HasValue())
// ...