如何检查去世的班级成员是否有价值? [英] How to check if deserealized class members have value?

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

问题描述

我有一个初始代码段,该代码段反序列化了参数,检查了值并处理了错误:

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);

现在,它使.HasValue和.Value方法无效。

Now it invalidates .HasValue and .Value methods.

我的问题是:我如何正确地修改这两行,使其与if语句的初始含义相同?

到目前为止,我只考虑过切换到 if(param!= null),但我想不出要与.Value适当地等效。

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

推荐答案

一旦 ClassName 是一个类(请参阅-引用类型),您只需检查它是否是不是 null

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.

这篇关于如何检查去世的班级成员是否有价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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