Int32
{
publicoverrideboolEquals(Object obj) {
if (!(obj is Int32)) {
returnfalse;
}
return m_value == ((Int32)obj).m_value;
}
[System.Runtime.Versioning.NonVersionable]
publicboolEquals(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.publicoverrideboolEquals(Object obj) {
if (!(obj is Double)) {
returnfalse;
}
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) {
returntrue;
}
return IsNaN(temp) && IsNaN(m_value);
}
publicboolEquals(Double obj)
{
if (obj == m_value) {
returntrue;
}
return IsNaN(obj) && IsNaN(m_value);
}
[System.Runtime.Versioning.NonVersionable]
publicstaticbooloperator ==(Double left, Double right) {
return left == right;
}
}
Single
{
publicoverrideboolEquals(Object obj) {
if (!(obj is Single)) {
returnfalse;
}
float temp = ((Single)obj).m_value;
if (temp == m_value) {
returntrue;
}
return IsNaN(temp) && IsNaN(m_value);
}
publicboolEquals(Single obj)
{
if (obj == m_value) {
returntrue;
}
return IsNaN(obj) && IsNaN(m_value);
}
[System.Runtime.Versioning.NonVersionable]
publicstaticbooloperator ==(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-generatedpublicoverrideboolEquals(Object value) {
if (valueis Decimal) {
Decimal other = (Decimal)value;
return FCallCompare(refthis, ref other) == 0;
}
returnfalse;
}
[System.Security.SecuritySafeCritical] // auto-generatedpublicboolEquals(Decimal value)
{
return FCallCompare(refthis, refvalue) == 0;
}
[System.Security.SecuritySafeCritical] // auto-generatedpublicstaticbooloperator ==(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)]
privatestaticexternintFCallCompare(ref Decimal d1, ref Decimal d2);
}
//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((object)a == (object)b);//返回false,下面解释为什么是false。这个是引用类型'==',放到下文介绍
*/
3.1.4、结论:对于简单常见值类型 int、float、double、decimal等,Equal函数 and 运算符’==’,如果其值相等,返回true;否则,返回false。
3.2、 结构体struct
3.2.1、 ValueType内部的Equals函数
ValueType
{
[System.Security.SecuritySafeCritical]
publicoverrideboolEquals (Object obj) {
BCLDebug.Perf(false, "ValueType::Equals is not fast. "+this.GetType().FullName+" should override Equals(Object)");
if (null==obj) {
returnfalse;
}
RuntimeType thisType = (RuntimeType)this.GetType();
RuntimeType thatType = (RuntimeType)obj.GetType();
if (thatType!=thisType) {
returnfalse;
}
Object thisObj = (Object)this;
Object thisResult, thatResult;
// if there are no GC references in this object we can avoid reflection // and do a fast memcmpif (CanCompareBits(this))
return FastEqualsCheck(thisObj, obj);
FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
for (int i=0; i<thisFields.Length; i++) {
thisResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(thisObj);
thatResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(obj);
if (thisResult == null) {
i
欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰富的快捷键快捷键加粗 Ctrl + B 斜体 Ctrl + I