GetType() and Typeof() in C#

后端 未结 5 1432
粉色の甜心
粉色の甜心 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:39

    Simply because you are comparing a byte with an int

    If you want to know number of bytes try this simple snippet:

    int i = 123456;
    Int64 j = 123456;
    byte[] bytesi = BitConverter.GetBytes(i);
    byte[] bytesj = BitConverter.GetBytes(j);
    Console.WriteLine(bytesi.Length);
    Console.WriteLine(bytesj.Length);
    

    Output:

    4
    8
    

提交回复
热议问题