.net里Equals 数字变量与数字常量的比较问题

前端之家收集整理的这篇文章主要介绍了.net里Equals 数字变量与数字常量的比较问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
VB.net代码
        Dim Integeri As Integer = 1
        Dim i16 As Int16 = 1
        Dim i32 As Int32 = 1
        Dim i64 As Int64 = 1

        Response.Write("i16=1;i32=1; Equals(i16,i32)=" + Equals(i16,i32).ToString + "<br/>")
        Response.Write("Integeri=1;i16=1; Equals(Integeri,i16)=" + Equals(Integeri,i16).ToString + "<br/>")
        Response.Write("Integeri=1;i32=1; Equals(Integeri,i32)=" + Equals(Integeri,i32).ToString + "<br/>")
        Response.Write("i16=1; Equals(i16,1))=" + Equals(i16,1).ToString + "<br/>")
        Response.Write(" i32=1;Equals(i32,1))=" + Equals(i32,1).ToString + "<br/>")
        Response.Write("i64=1; Equals(i64,1))=" + Equals(i64,1).ToString + "<br/>")

        i64 = 2147483649
        Response.Write("i64 = 2147483649; Equals(i64,2147483649)=" + Equals(i64,2147483649).ToString + "<br/>")

输出结果,

i16=1;i32=1; Equals(i16,i32)=False

Integeri=1;i16=1; Equals(Integeri,i16)=False
Integeri=1;i32=1; Equals(Integeri,i32)=True
i16=1; Equals(i16,1))=False
i32=1;Equals(i32,1))=True
i64=1; Equals(i64,1))=False
i64 = 2147483649; Equals(i64,2147483649)=True


结论:

'1,Equals 比较不只比较值,也比较类型,只有值同,而且类型也相同,才会认为相等
'2,Integer 其实是 Int32的别名,而不是等于机器位长。
'3,数字常量是有类型的:在Int32类型表示范围内时,常量的类型是Int32;超出范围,常量的类型就是Int64
'4,用Equals比较变量与数字常量时,要特别注意类型匹配问题

猜你在找的VB相关文章