如何检查该值是否为空 [英] How to check if the value is null or not
问题描述
get {
if (statusMessage.Message =!null)
return statusMessage.Message;
}
我需要先检查状态消息是否为空,然后再返回.但是当我检查它时,出现以下错误.那我该如何检查它是否为空呢?
I need to check if the status message is null before returning it. but when I check it it give me the following error. then How can i check if its null or not?
StatusMessage.Message cannot be assigned to -- it is read only<br />
<br />
Error 3 Cannot implicitly convert type ''bool?'' to ''string'' <br />
Error 4 Cannot implicitly convert type ''string'' to ''bool''
推荐答案
您正在将!null
(未编译)分配给statusMessage.Message
,您需要测试statusMessage
表示null,然后statusMessage.Message
表示
You''re assigning!null
(which doesn''t compile) tostatusMessage.Message
You need to teststatusMessage
for null and thenstatusMessage.Message
for null
if (statusMessage != null)
!
必须在=
的左侧
哦,在statusMessage
为null的情况下,您还需要返回其他内容
The !
has to be on the left side of the =
Oh and one more thing you need to return something else in the case when statusMessage
is null
get
{
if (statusMessage != null)
return statusMessage.Message;
return string.Empty; // assuming statusMessage.Message is a string
}
假设statusMessage
永远不会为null,您将像这样
Assuming statusMessage
is never null you would do it like so
get
{
if (statusMessage.Message != null) // If statusMessage can be null it must be tested aswell
return statusMessage.Message;
return string.Empty; // assuming statusMessage.Message is a string
}
请记住,在statusMessage.Message
为null
的情况下,您必须返回其他值.
并且!
必须位于=
Remember you have to return some other value in the case when statusMessage.Message
is null
.
And the !
has to be on the left side of the =
的左侧,只需使用
simply use
if(string.IsNullOrEmpty(statusMessage.Message))
return statusMessage.Message;
使用!=代替=!.
Use != instead of =!.
if (statusMessage.Message != null)
这篇关于如何检查该值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!