سؤال

If I implement IEquatable(Of T) on my class and let Visual Studio (2010) auto-generate the required Equals method, I get this:

Public Function Equals1(ByVal other As Foo) As Boolean _
  Implements System.IEquatable(Of Foo).Equals

End Function

Notice the extra 1 necessary due to the naming conflict with Object.Equals. Are there any conventions/suggestions for naming this function something other than Equals1? Because frankly, that's rather ugly.

هل كانت مفيدة؟

المحلول

Defining a method with the same name as a base-type method but a different signature can have two different effects:

  1. It may make the base-type method inaccessible through the derived type, so that only the definitions in the derived type will be considered.
  2. It may add the new method definitions to list of definitions imported from the base type.

There are some scenarios where the first option is correct, and there are some where the second option is correct. Rather than guessing which option should be used, VB.NET requires that when creating a function with the same name as a base-type function but a different signature, the programmer must either the Shadows keyword to specify the first behavior, or the Overloads keyword to specify the second.

Although Microsoft auto-generates interface implementations using public methods, in some cases one may want to have a method only be available through an interface. Because VB allows the name of the function to be specified independently from the name of the implemented interface member one can make interface member be accessible only via the implementation by making it private and giving it any name which isn't going to conflict with anything else used within the class itself (if the method is private, conflicts with derived-class members won't be an issue).

Pick an arbitrary non-conflicting name while leaving the declaration as public is probably almost never the right course of action [one should either change the declaration to private, change the declaration to Protected Overridable and change the name to something better (e.g. SomeMember_Impl), or else change the name to match the interface member and add either an Overloads or Shadows keyword as appropriate. In the case of IEquatable(Of T).Equals, the Overloads keyword is probably the best approach.

Incidentally, IEquatable(Of T) should generally only be implemented by sealed classes (or structures, which are inherently sealed). It can provide a big performance win when applied to structures (it saves a boxing operation every time it's used), and a small performance win when applied to sealed classes (there's no need to check the type of its operand). When applied to unsealed types, it's difficult to ensure that for all derivative types the semantics of IEquatable(Of T).Equals(T) will match those of Equals(Object) except by having the former method chain to the latter. Such chaining would negate any performance advantage that could have been gained by implementing IEquatable(Of T) in the first place.

نصائح أخرى

You should be able to name it Equals(); that will merely overload the Equals(Object) method.
You may need to add the Overloads keyword.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top