我想知道如何比较两个对象(属于同一类),如string.Compare()
方法。
有没有办法做到这一点?
答案 0 :(得分:7)
您可以实施IComparable
界面,例如sugested here:
public class Temperature : IComparable
{
// The temperature value
protected double temperatureF;
public int CompareTo(object obj) {
if (obj == null) return 1;
Temperature otherTemperature = obj as Temperature;
if (otherTemperature != null)
return this.temperatureF.CompareTo(otherTemperature.temperatureF);
else
throw new ArgumentException("Object is not a Temperature");
}
...
您将使用CompareTo
方法比较您班级的项目。有关IComparable
的更多信息,请访问SO。拥有CompareTo
后,您可以根据比较函数对对象列表进行排序,如提到的here
答案 1 :(得分:1)
您需要实现IComparable接口。
private class sortYearAscendingHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
car c1=(car)a;
car c2=(car)b;
if (c1.year > c2.year)
return 1;
if (c1.year < c2.year)
return -1;
else
return 0;
}
}
可以找到原始帖子 Here
答案 2 :(得分:1)
由于对象是引用类型,因此您应该使用 obj.Equals()方法。 还要注意:
string.ReferenceEquals(str, str2);
它显然比较了参考文献。
str.Equals(str2)
首先尝试比较参考文献。然后它尝试按值进行比较。
str == str2
与Equals相同。
答案 3 :(得分:1)
您可以检查两个相等的参考平等和价值平等
参考平等
引用相等意味着两个对象引用引用相同的底层对象。这可以通过简单的赋值来实现,如以下示例所示。
class Test
{
public int Num { get; set; }
public string Str { get; set; }
static void Main()
{
Test a = new Test() { Num = 1, Str = "Hi" };
Test b = new Test() { Num = 1, Str = "Hi" };
bool areEqual = System.Object.ReferenceEquals(a, b);
// Output will be false
System.Console.WriteLine("ReferenceEquals(a, b) = {0}", areEqual);
// Assign b to a.
b = a;
// Repeat calls with different results.
areEqual = System.Object.ReferenceEquals(a, b);
// Output will be true
System.Console.WriteLine("ReferenceEquals(a, b) = {0}", areEqual);
}
}
价值平等
值相等意味着两个对象包含相同的值。对于原始值类型(如int或bool),值相等的测试很简单。
答案 4 :(得分:0)
比较两个对象请访问
答案 5 :(得分:0)
public class BaseEntity
{
public int Id { get; set; }
}
public class Product : BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
//Generic Comparer
public class EntityComparer<T> : IEqualityComparer<T>, IComparer<T>
where T : BaseEntity
{
public enum AscDesc : short
{
Asc, Desc
}
#region Const
public string PropertyName { get; set; }
public AscDesc SortType { get; set; }
#endregion
#region Ctor
public EntityComparer(string _propertyname = "Id", AscDesc _sorttype = AscDesc.Asc)
{
this.PropertyName = _propertyname;
this.SortType = _sorttype;
}
#endregion
#region IComparer
public int Compare(T x, T y)
{
if (typeof(T).GetProperty(PropertyName) == null)
throw new ArgumentNullException(string.Format("{0} does not contain a property with the name \"{1}\"", typeof(T).Name, PropertyName));
var xValue = (IComparable)x.GetType().GetProperty(this.PropertyName).GetValue(x, null);
var yValue = (IComparable)y.GetType().GetProperty(this.PropertyName).GetValue(y, null);
if (this.SortType == AscDesc.Asc)
return xValue.CompareTo(yValue);
return yValue.CompareTo(xValue);
}
#endregion
#region IEqualityComparer
public bool Equals(T x, T y)
{
if (typeof(T).GetProperty(PropertyName) == null)
throw new InvalidOperationException(string.Format("{0} does not contain a property with the name -> \"{1}\"", typeof(T).Name, PropertyName));
var valuex = x.GetType().GetProperty(PropertyName).GetValue(x, null);
var valuey = y.GetType().GetProperty(PropertyName).GetValue(y, null);
if (valuex == null) return valuey == null;
return valuex.Equals(valuey);
}
public int GetHashCode(T obj)
{
var info = obj.GetType().GetProperty(PropertyName);
object value = null;
if (info != null)
{
value = info.GetValue(obj, null);
}
return value == null ? 0 : value.GetHashCode();
}
#endregion
}
//Usage
Product product = new Product();
Product product2 =new Product();
EntityComparer<Product> comparer = new EntityComparer<Product>("Propert Name Id Name whatever u want", Asc Or Desc);
comparer.Compare(product, product2);