1、==、!=、<、>、<= 和>= 运算符为比较运算符(comparison operator)。C#语言规范5.0中文版中比较运算符的描述如下:
2、通用类型系统
3、值类型Equal函数 and 运算符'=='
3.1、常见类型 int、float、double、decimal等虽然继承自ValueType,但其结构体内部重写了Equal。
3.1.1、 int,float,double,decimal内部的Equal函数和 '=='重载符函数。
Int32
{
public override bool Equals(Object obj) {
if (!(obj is Int32)) {
return false;
}
return m_value == ((Int32)obj).m_value;
}
[System.Runtime.Versioning.NonVersionable]
public bool Equals(Int32 obj)
{
return m_value == obj;
}
}
Double
{
// True if obj is another Double with the same value as the current instance. This is
// a method of object equality, that only returns true if obj is also a double.
public override bool Equals(Object obj) {
if (!(obj is Double)) {
return false;
}
double temp = ((Double)obj).m_value;
// This code below is written this way for performance reasons i.e the != and == check is intentional.
if (temp == m_value) {
return true;
}
return IsNaN(temp) && IsNaN(m_value);
}
public bool Equals(Double obj)
{
if (obj == m_value) {
return true;
}
return IsNaN(obj) && IsNaN(m_value);
}
[System.Runtime.Versioning.NonVersionable]
public static bool operator ==(Double left, Double right) {
return left == right;
}
}
Single
{
public override bool Equals(Object obj) {
if (!(obj is Single)) {
return false;
}
float temp = ((Single)obj).m_value;
if (temp == m_value) {
return true;
}
return IsNaN(temp) && IsNaN(m_value);
}
public bool Equals(Single obj)
{
if (obj == m_value) {
return true;
}
return IsNaN(obj) && IsNaN(m_value);
}
[System.Runtime.Versioning.NonVersionable]
public static bool operator ==(Single left, Single right) {
return left == right;
}
}
Decimal
{
// Checks if this Decimal is equal to a given object. Returns true
// if the given object is a boxed Decimal and its value is equal to the
// value of this Decimal. Returns false otherwise.
//
[System.Security.SecuritySafeCritical] // auto-generated
public override bool Equals(Object value) {
if (value is Decimal) {
Decimal other = (Decimal)value;
return FCallCompare(ref this, ref other) == 0;
}
return false;
}
[System.Security.SecuritySafeCritical] // auto-generated
public bool Equals(Decimal value)
{
return FCallCompare(ref this, ref value) == 0;
}
[System.Security.SecuritySafeCritical] // auto-generated
public static bool operator ==(Decimal d1, Decimal d2) {
return FCallCompare(ref d1, ref d2) == 0;
}
//暂时不知道此函数内部代码,如有知道还望告知。
//根据测试结果,推测如果两个decimal数相等,返回0
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
private static extern int FCallCompare(ref Decimal d1, ref Decimal d2);
}
3.1.3、测试代码:
//T is int 、float、double、decimal、byte、char
T a = 1234567890;//0.1234567890f、0.123456789、1234567890M、(byte)11、'a'
T b = 1234567890;//0.1234567890f、0.123456789、1234567890M、(byte)11、'a'
Console.WriteLine(a == b);//返回true
Console.WriteLine(a.Equals(b));//返回true
Console.WriteLine(a.Equals((object)b));//返回true
/*
Console.WriteLine((object)a == b);//编译错误:运算符‘==’无法应用与‘object’和‘T’类型操作数
Console.WriteLine(a == (object)b);//编译错误:运算符‘==’无法应用与‘object’和‘T’类型操作数
//Console.WriteLine((obj