'c# parsing null to int from api fails [duplicate]
I am trying to parse a null from our api into an int like this:
API:
"thresholdMax": 10,
"thresholdAvg": null,
"thresholdMin": 40
Datamodel:
public int thresholdMax { get; set; }
public int thresholdAvg
{
get { return thresholdAvg; }
set
{
try
{
thresholdAvg = value;
}
catch
{
thresholdAvg = 0;
}
}
}
public int thresholdMin { get; set; }
But it fails saying it cannot parse null to int. I though however that with how I am setting these values, it should work.
What is the best practice, if I dont want to convert the type to string?
Solution 1:[1]
if you want to allow null values you could easily do:
public int? thresholdAvg { get; set; }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel |
