比较两个对象的 C# 问题
C# issue comparing two objects
我正在尝试比较 2 个具有属性和子属性的对象。即使对象值匹配,下面的代码也总是 return false。在这种情况下比较的最佳选择是什么?
Student a = new Student();
a.Name = "john";
a.Address ="CA";
//a.StudDetails.Location = "LA";
//a.StudDetails.Study = "MBA";
//a.StudDetails.Friends = new string[] { "X", "Y"};
Student b = new Student();
b.Name = "john";
b.Address = "CA";
//b.StudDetails.Location = "LA";
//b.StudDetails.Study = "MBA";
//b.StudDetails.Friends = new string[] { "X", "Y"};
bool x = Equals(a, b);
if (x)
{
Console.WriteLine("matched");
}
else
{
Console.WriteLine("Not Matched");
}
public class Student
{
public string Name;
public Details StudDetails;
public string Address;
}
public class Details
{
public string Study;
public string Location;
public string[] Friends;
}
您必须对 Student 实施 Equals https://msdn.microsoft.com/en-us/library/336aedhh(v=vs.100).aspx
public class Details
{
public string Study;
public string Location;
public string[] Friends;
}
public class Student
{
public string Name;
public Details StudDetails;
public string Address;
public override bool Equals(Object obj)
{
// Check for null values and compare run-time types.
if (obj == null || GetType() != obj.GetType())
return false;
Student s = (Student)obj;
return (Name == s.Name) && (Address == s.Address);
}
}
Student a = new Student();
a.Name = "john";
a.Address ="CA";
//a.StudDetails.Location = "LA";
//a.StudDetails.Study = "MBA";
//a.StudDetails.Friends = new string[] { "X", "Y"};
Student b = new Student();
b.Name = "john";
b.Address = "CA";
//b.StudDetails.Location = "LA";
//b.StudDetails.Study = "MBA";
//b.StudDetails.Friends = new string[] { "X", "Y"};
bool x = a.Equals(b);
Console.WriteLine( x );
此代码打印 "True".
我正在尝试比较 2 个具有属性和子属性的对象。即使对象值匹配,下面的代码也总是 return false。在这种情况下比较的最佳选择是什么?
Student a = new Student();
a.Name = "john";
a.Address ="CA";
//a.StudDetails.Location = "LA";
//a.StudDetails.Study = "MBA";
//a.StudDetails.Friends = new string[] { "X", "Y"};
Student b = new Student();
b.Name = "john";
b.Address = "CA";
//b.StudDetails.Location = "LA";
//b.StudDetails.Study = "MBA";
//b.StudDetails.Friends = new string[] { "X", "Y"};
bool x = Equals(a, b);
if (x)
{
Console.WriteLine("matched");
}
else
{
Console.WriteLine("Not Matched");
}
public class Student
{
public string Name;
public Details StudDetails;
public string Address;
}
public class Details
{
public string Study;
public string Location;
public string[] Friends;
}
您必须对 Student 实施 Equals https://msdn.microsoft.com/en-us/library/336aedhh(v=vs.100).aspx
public class Details
{
public string Study;
public string Location;
public string[] Friends;
}
public class Student
{
public string Name;
public Details StudDetails;
public string Address;
public override bool Equals(Object obj)
{
// Check for null values and compare run-time types.
if (obj == null || GetType() != obj.GetType())
return false;
Student s = (Student)obj;
return (Name == s.Name) && (Address == s.Address);
}
}
Student a = new Student();
a.Name = "john";
a.Address ="CA";
//a.StudDetails.Location = "LA";
//a.StudDetails.Study = "MBA";
//a.StudDetails.Friends = new string[] { "X", "Y"};
Student b = new Student();
b.Name = "john";
b.Address = "CA";
//b.StudDetails.Location = "LA";
//b.StudDetails.Study = "MBA";
//b.StudDetails.Friends = new string[] { "X", "Y"};
bool x = a.Equals(b);
Console.WriteLine( x );
此代码打印 "True".