Vadzim Savenok Vadzim Savenok - 10 months ago 53
C# Question

How to check if deserealized class members have value?

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;
//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.

Answer Source

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.