问题描述
在Java中,如果要通过remove()
方法从通用Collection
中正确删除对象,则必须实现equals(Object o)
和remove()
方法,它们可以在Eclipse中自动生成.该方法的示例如下所示--->.
In Java when you want to have remove correctly object from a generic Collection
by remove()
method you have to implement equals(Object o)
and remove()
method which can be automatically generated in Eclipse. Example of that method looks like that ---> below.
-
如何在C#中自动生成该方法(Visual Studio,我在VS2013上)?
How to automatically generate that method in C# (Visual Studio, I'm on VS2013)?
也许没有必要使List.Remove()
方法正常工作?
Maybe it is not necessary to make List.Remove()
method working properly?
如果无法自动引用Equals
方法应如何?我的意思是它的外观.
IF it is not possible automatically how the reference Equals
methods should look like? I mean how it should look like.
List.Remove()
是否甚至在List.Remove()
中使用了Equals()
方法,如果您能告诉我,如果我们比较相同的对象(内存中的相同地址),应该如何实现Equals()
以返回true
Is Equals()
method is even used in List.Remove()
if so could you show me how the Equals()
should be implemented to return true if we compare THE SAME OBJECTS (same address in memory)
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((centerPanel == null) ? 0 : centerPanel.hashCode());
result = prime * result + ((lowerPanel == null) ? 0 : lowerPanel.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
LayoutDemo other = (LayoutDemo) obj;
if(centerPanel == null) {
if(other.centerPanel != null)
return false;
} else if(!centerPanel.equals(other.centerPanel))
return false;
if(lowerPanel == null) {
if(other.lowerPanel != null)
return false;
} else if(!lowerPanel.equals(other.lowerPanel))
return false;
return true;
}
推荐答案
在设计时自动生成Equals()
如果您想一次生成它,然后手动维护生成的源代码(例如,如果类更改),那么Resharper是一个有用的工具,就像@ThomasWeller在其答案中已经提到的那样.
Automatic Equals()
generation at design time
If you want to generate it once and then maintain the generated source code manually (e.g. if the class changes), Resharper is a useful tool, as @ThomasWeller already mentioned in his answer.
请注意,这种方法有可能难以发现错误,因为在更改类时,您需要记住要适应Equals()
实现.为避免这种情况,请使用以下方法:
Note that this approach has potential for hard to find bugs, because you need to remember to adapt the Equals()
implementation when you change the class. To avoid this, use the following approach:
如果您想要一个在运行时动态生成Equals()
和GetHashCode()
方法的解决方案,则可以使用 Equ (我是该库的作者). Equ在静态初始化时生成相等方法并将其缓存,因此在静态初始化之后,性能与显式实现相同.
If you want a solution that dynamically generates Equals()
and GetHashCode()
methods at runtime, you can use Equ (I'm the author of that library). Equ generates the equality methods at static initialization time and caches them, so after static initialization, performance is the same as an explicit implementation.
这是一个简单的示例:
class Address : MemberwiseEquatable<Address>
{
public Address(string street, string city)
{
Street = street;
City = city;
}
public string Street { get; }
public string City { get; }
}
因此,以下表达式为true
:
new Address("Baker Street", "London") == new Address("Baker Street", "London")
这是使用Equ最简单的方法:只需从MemberwiseEquatable<TSelf>
继承即可.请注意,如果您不能/不希望从基类继承,则还有其他可能性.
This is the easiest way of using Equ: Just inherit from MemberwiseEquatable<TSelf>
. Note that there are other possibilities if you can not / don't want to inherit from a base class.
在最后一个问题中,您想知道如何编写Equals
方法,该方法通过内存中的地址"比较对象.这称为引用相等比较,它是每个类均从object
继承的默认Equals()
实现.因此,要在类上获得引用相等,只需不要覆盖Equals()
.
In your last question you want to know how to write an Equals
method that compares objects by "address in memory". This is called reference equality comparison and is the default Equals()
implementation that every class inherits from object
. So to get reference equality on your class, just don't override Equals()
.
但是,您应该仔细考虑要通过引用进行比较的对象,以及要通过值进行比较的对象.如果您使用域驱动的设计术语,则应按值比较值对象,而应按引用或ID比较实体.
You should however think carefully about which objects you want to compare by reference, and which you want to compare by value. If you use the domain-driven design terminology, value objects should be compared by value, whereas entities should be compared by reference or by ID.
这篇关于有没有一种方法可以在Visual Studio中自动生成equals和hashcode方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!