gpt4 book ai didi

C# IF 语句给出不正确的结果

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:56 25 4
gpt4 key购买 nike

我似乎偶然发现了 C# IF 语句给出错误结果的情况。我试图编写一个 Equals() 版本来深入比较一个类的两个实例。

这是一个带有一些调试的简单测试用例:

namespace IfTest
{
class MyClass
{
public string String1
{ get; set; }
public int Int1
{ get; set; }
public float Float1
{ get; set; }
public bool Bool1
{ get; set; }

public MyClass(string s, int i, float f, bool b)
{
String1 = s;
Int1 = i;
Float1 = f;
Bool1 = b;
}

public override bool Equals(object otherInstance)
{
bool isEqual = true;
MyClass other = (MyClass)otherInstance;
int good = 0, bad = 0;

// Compare everything in 'other' to 'this', using reflection
Type sourceType = other.GetType();
Type destinationType = this.GetType();

foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
{
PropertyInfo destinationProperty = destinationType.GetProperty(sourceProperty.Name);
if (destinationProperty == null)
{
Console.WriteLine("Destination {0} is null", sourceProperty.Name);
isEqual = false;
bad++;
}
else
{
var x = sourceProperty.GetValue(other);
var y = destinationProperty.GetValue(this);
//if (sourceProperty.GetValue(other, null) != destinationProperty.GetValue(this, null))

if (x != y)
{
Console.WriteLine("Name {0}: {1} {2} different", sourceProperty.Name, x, y);
isEqual = false;
bad++;
}
else
{
Console.WriteLine("Name {0}: {1} {2} same", sourceProperty.Name, x, y);
good++;
}
}
}

Console.WriteLine("Good: {0}. Bad {1}", good, bad);
return isEqual;
}
}
}


using System;

namespace IfTest
{
class Program
{
static void Main(string[] args)
{
MyClass a = new MyClass("abc", 23, 45.67F, false);
MyClass b = new MyClass("abc", 23, 45.67F, false);

// Test IF usually works with var's
var i = a.Int1;
var j = b.Int1;
Console.WriteLine("Main test {0}",
(i == j) ? "OK" : "Fail");

a.Equals(b);

Console.ReadLine();
}

}
}

在 MyClass.Equals() 我已经注释掉了这一行 if (sourceProperty.GetValue(other, null) != destinationProperty.GetValue(this, null))

并将 2 个属性值放入临时变量中。

运行这个给出:

Main test OK
Name String1: abc abc same
Name Int1: 23 23 different
Name Float1: 45.67 45.67 different
Name Bool1: False False different
Good: 1. Bad 3

这表明 IF 对数字类型失败。如果我将 x 和 y 赋值更改为: var x = sourceProperty.GetValue(other); var y = sourceProperty.GetValue(other);

我可以通过添加如下内容来解决它:

if (x is int)
{
if ((int) x == (int)y)
Console.WriteLine("INT Name {0}: {1} {2} same", sourceProperty.Name, x, y);
}

但我必须测试每种数字类型。

这是 C# 问题还是我做了什么傻事?我正在使用 Visual Studio Express 2013 for Desktop 版本 12.0.31101.00 Update 4、.Net 版本 4.5.50938 和 Visual C# 2013。

最佳答案

比较失败是因为您在 Object 类型上使用了 == 运算符 - 它执行引用相等操作而不是值相等操作,此外.NET Framework 中的反射 API 不会缓存和重新返回预生成的反射对象实例,这就是返回 false 的原因:

foo.GetType().GetMethod("Bar") == foo.GetType().GetMethod("Bar")

另一个问题是您不恰本地使用了 var 关键字:它隐藏了您正在装箱值类型的事实。 xy 的类型是盒装的 int 作为 Object,因此它执行引用相等性比较的原因而不是值(value)平等。

解决方案是在引用类型上显式调用 .Equals 方法,并通过强制转换为它们的真实类型来拆箱值类型:

foo.GetType().GetMethod("Bar").Equals( foo.GetType().GetMethod("Bar") )

Int32 x = (Int32)sourceProperty.GetValue(other);

关于C# IF 语句给出不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29833650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com