When must we use checked operator in C#?

前端 未结 4 1813
野趣味
野趣味 2021-01-04 05:22

When must we use checked operator in C#?
Is it only suitable for exception handling?

4条回答
  •  一向
    一向 (楼主)
    2021-01-04 05:37

    checked will help you to pick up System.OverFlowException which will go unnoticed otherwise

    int result = checked (1000000 * 10000000);   
        // Error: operation > overflows at compile time
    
    int result = unchecked (1000000 * 10000000);  
        // No problems, compiles fine
    

提交回复
热议问题