Cannot implicitly convert type 'int?' to 'int'.

前端 未结 10 1538
野性不改
野性不改 2021-01-03 19:34

I\'m getting the error \"Cannot implicitly convert type \'int?\' to \'int\'. An explicit conversion exists (are you missing a cast?)\" on my OrdersPerHour at the return line

10条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 20:32

    this is because the return type of your method is int and OrdersPerHour is int? (nullable) , you can solve this by returning its value like below:

    return OrdersPerHour.Value
    

    also check if its not null to avoid exception like as below:

    if(OrdersPerHour != null)
    {
    
        return OrdersPerHour.Value;
    
    }
    else
    {
    
      return 0; // depends on your choice
    
    }
    

    but in this case you will have to return some other value in the else part or after the if part otherwise compiler will flag an error that not all paths of code return value.

提交回复
热议问题