创建共享的 IEqualityComparer
我正在做一些需要自定义比较器的 LINQ,因此我创建了一个实现 IEqualityComparer
的新类。但是,当我使用它时,我每次都必须创建它的实例。
Dim oldListOnly = oldList.Except(newList, New MyEqualityComparer)
Dim newListOnly = newList.Except(oldList, New MyEqualityComparer)
我可能误解了 .NET 的工作原理,但每次创建一个新的比较器似乎很浪费。我真的只想要一个实例(相当于 C++/C# 中的 static)。
所以我尝试创建一个“静态”类,它在 vb.net 中是一个模块。但出现“Implements”在模块中无效
错误。
然后,我尝试在我的类上创建 Equals 和 GetHashCode 函数共享方法,但收到此错误: 实现接口成员的方法不能声明为“共享”。
有什么想法可以在这里实现我的目标吗?或者我只是误解了幕后发生的事情?
I'm doing some LINQ which requires a custom comparer, so I created a new class implementing IEqualityComparer
. However, when I use it, I have to create an instance of it each time.
Dim oldListOnly = oldList.Except(newList, New MyEqualityComparer)
Dim newListOnly = newList.Except(oldList, New MyEqualityComparer)
I may be misunderstanding how .NET works, but it seems wasteful to create a new comparer each time. I really just want one instance (the equivalent of static in C++/C#).
So I tried creating a "static" class, which in vb.net is a module. But got an 'Implements' not valid in Modules
error.
I then tried making the Equals and GetHashCode function shared methods on my class, but got this error: Methods that implement interface members cannot be declared 'Shared'.
Any ideas how to accomplish my goal here? Or am I simply misunderstanding what's going behind the scenes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的理解是正确的,尽管浪费不太明显。对于您的情况,您可以使用单例模式,通常如下所示:
Your understanding is correct, although the waste is unlikely to be noticeable. For your situation, you could use the singleton pattern, which usually goes something like this:
为什么不简单地做
Why not simply do
需要有一个实现
IEqualityComparer
的具体类型的实例。然而,您可以对模块执行的操作是定义一个初始化为“New EqualityComparer”的公共实例。然后,您可以将该默认实例传递给 except 方法。像这样的东西:
There needs to be an instance of a concrete type that implements
IEqualityComparer
. What you can do with a module, however, is define a public instance which is initialized to "New EqualityComparer". You can then pass that default instance to the Except method.Something like: