GetType() and Typeof() in C#

后端 未结 5 1431
粉色の甜心
粉色の甜心 2021-01-21 20:17
itemVal = \"0\";

res = int.TryParse(itemVal, out num);

if ((res == true) && (num.GetType() == typeof(byte)))  
    return true;
else
   return false;  // goes          


        
5条回答
  •  感情败类
    2021-01-21 20:21

    If you wanna check if this int value would fit a byte, you might test the following;

    int num = 0;
    byte b = 0;
    
    if (int.TryParse(itemVal, out num) && byte.TryParse(itemVal, b))
    {
        return true; //Could be converted to Int32 and also to Byte
    }
    

提交回复
热议问题