C# 如果为空则为空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1660269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Empty string if null
提问by Lasse Edsvik
I have this in my code:
我的代码中有这个:
SelectList(blah, "blah", "blah", cu.Customer.CustomerID.ToString())
It gives a error when it returns null, how can I make it CustomerID is an empty string if it is null?
它在返回 null 时会出错,如果 CustomerID 为 null,我怎样才能使它的 CustomerID 为空字符串?
/M
/M
采纳答案by Groo
(Update for C# 6.0)
(针对 C# 6.0 的更新)
If you are using C# 6 or newer (Visual Studio 2015 or newer), then you can achieve this using the null-conditional operator ?.
:
如果您使用的是 C# 6 或更高版本(Visual Studio 2015 或更高版本),那么您可以使用空条件运算符?.
来实现这一点:
var customerId = cu.Customer?.CustomerId.ToString() ?? "";
One useful property of the null-conditional operator is that it can also be "chained" if you want to test if several nested properties are null:
null 条件运算符的一个有用属性是,如果您想测试多个嵌套属性是否为 null,它也可以“链接”:
// ensure (a != null) && (b != null) && (c != null) before invoking
// a.b.c.CustomerId, otherwise return "" (short circuited at first encountered null)
var customerId = a?.b?.c?.CustomerId.ToString() ?? "";
For C# versions prior to 6.0(VS2013 or older), you could coalesce it like this:
对于6.0 之前的 C# 版本(VS2013 或更早版本),您可以像这样合并它:
string customerId = cu.Customer != null ? cu.Customer.CustomerID.ToString() : "";
Simply check if the object is non-null before you try to access its members, and return an empty string otherwise.
在尝试访问其成员之前,只需检查对象是否为非空,否则返回空字符串。
Apart from that, there are situations where null objectpattern is useful. That would mean that you ensure that your Customer
's parent class (type of cu
in this case) alwaysreturn an actual instance of an object, even if it is "Empty". Check this link for an example, if you think it may apply to your problem: How do I create a Null Object in C#.
除此之外,在某些情况下空对象模式很有用。这意味着您要确保Customer
的父类(cu
在本例中为类型)始终返回对象的实际实例,即使它是“空”。如果您认为它可能适用于您的问题,请查看此链接的示例:How do I create a Null Object in C#。
回答by Palantir
SelectList(blah, "blah", "blah",
(cu.Customer.CustomerID!=null?cu.Customer.CustomerID.ToString():"")
)
回答by adrianbanks
It depends of the type of CustomerID
.
这取决于CustomerID
.
If CustomerID
is a string then you can use the null coalescing operator:
如果CustomerID
是字符串,则可以使用空合并运算符:
SelectList(blah, "blah", "blah", cu.Customer.CustomerID ?? string.Empty)
If CustomerID
is a Nullable<T>
, then you can use:
如果CustomerID
是 a Nullable<T>
,那么您可以使用:
SelectList(blah, "blah", "blah", cu.Customer.CustomerID.ToString())
This will work because the ToString()
method of Nullable<T>
returns an empty string if the instance is null
(technically if the HasValueproperty is false
).
这将起作用,因为如果实例是(技术上如果HasValue属性是),则ToString()
方法Nullable<T>
返回一个空字符串。null
false
回答by DKroot
(C# 2.0 - C# 5.0)
(C# 2.0 - C# 5.0)
The ternary operator works, but if you want even shorter expression working on arbitrary objects you can use:
三元运算符有效,但如果您想要处理任意对象的更短的表达式,您可以使用:
(myObject ?? "").ToString()
Here is real-life example from my code:
这是我的代码中的真实示例:
private HtmlTableCell CreateTableCell(object cellContents)
{
return new HtmlTableCell()
{
InnerText = (cellContents ?? "").ToString()
};
}