我有一行简单的代码:lead.InternalCompany = nvCollection["ic"];
我想将 lead.InternalCompany
设置为检索到的值,但如果没有空字符串 ""
我尝试使用 nvCollection["ic"].HasValue();
我知道我可以做这样简单的事情
string value = nvCollection["ic"];
if (value == null) // key doesn't exist
{
lead.InternalCompany = "";
}
理想情况下,我希望使用三元 if 语句
来完成此操作
最佳答案
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.
lead.InternalCompany = nvCollection["ic"] ?? string.Empty;
关于c# - 检查 NameValueCollection 是否有值(value)的最优雅的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18383892/