如何检查该值是否为空 [英] How to check if the value is null or not

查看:100
本文介绍了如何检查该值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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) to statusMessage.Message You need to test statusMessage for null and then statusMessage.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.Messagenull的情况下,您必须返回其他值.
并且!必须位于=


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆