Why this code doesn't run, I want to check if JSON contains integer for key PurchasedValue
or not? () :
public PropertyInfo(Newtonsoft.Json.Linq.JToken jToken)
{
this.jToken = jToken;
int PurchasedValue = (int)(jToken["PurchasedValue"].Value ?? 0);
}
the error is :
Error CS0019: Operator `??' cannot be applied to operands of type `method group' and `int' (CS0019)
From my understanding jToken["PurchasedValue"]
is a nullable value.
You have to use
int PurchasedValue = (int)(jToken["PurchasedValue"]?? 0);
nullableObj.Value
can be only used without error only when there is a value for the nullableObj
Otherwise You can use like
int PurchasedValue = jToken["PurchasedValue"].HasValue?jToken["PurchasedValue"].Value: 0;
This May not even need type casting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With