How do I convert uint to int in C#?

前端 未结 8 1486
时光说笑
时光说笑 2020-12-05 03:54

How do I convert uint to int in C#?

8条回答
  •  春和景丽
    2020-12-05 04:05

    Given:

     uint n = 3;
    
    int i = checked((int)n); //throws OverflowException if n > Int32.MaxValue
    int i = unchecked((int)n); //converts the bits only 
                               //i will be negative if n > Int32.MaxValue
    
    int i = (int)n; //same behavior as unchecked
    

    or

    int i = Convert.ToInt32(n); //same behavior as checked
    

    --EDIT

    Included info as mentioned by Kenan E. K.

提交回复
热议问题