How to check if deserealized class members have value?

99封情书 提交于 2019-12-25 07:59:39

问题


I have an initial snippet which deserealized parameters, checks for value and handles the error:

var param = js.Deserialize<int?>(jqData.Params);


if (param.HasValue)
{
     resp.ReturnValue = param.Value;
}
else
{
     //handle error code
}

Now, during my modification, I have changed the method to accept a list of class parameters instead of nullable integers

var param = js.Deserialize<ClassName>(jqData.Params);

Now it invalidates .HasValue and .Value methods.

My question is: How do I properly modify these two lines so it would hold the same meaning as initial if statement?

Thus far I only thought about switching to if (param != null), but I cannot think of proper equivalent to .Value.


回答1:


As soon as ClassName is a class (see - a reference type) you just need to check if it is not null.

If it is not - then the variable holds a reference to an object that you use as-is.



来源:https://stackoverflow.com/questions/39967822/how-to-check-if-deserealized-class-members-have-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!