3

I was converting text to ASCII number in Powershell and having trouble with ToByte(). When I looked at the methods for string, I see that some of them show up with an interface prefix, whilst most don't.

Can anyone tell me the difference between these defintions?
Why ToByte() starts with IConvertible, but PadLeft() doesn't?
Any why ToString() has both of these notations?

screenshot

Destrif
  • 2,104
  • 1
  • 14
  • 22
  • Possible duplicate of [.net Interface explanation](http://stackoverflow.com/questions/2021777/net-interface-explanation) – TessellatingHeckler Jul 06 '16 at 16:25
  • @TessellatingHeckler I don't think that question or its answers address the reason why specific methods of the String class are shown with explicit interface names while others are not. – briantist Jul 06 '16 at 16:36

1 Answers1

3

Because those methods with the interface name are Explicit Interface Implementations.

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period.

Explicit implementation is also used to resolve cases where two interfaces each declare different members of the same name such as a property and a method.

You can also see this listed in the String class documentation under Explicit Interface Implementations.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • Thanks for the links. Comparing String class documentation and behavior with Powershell, it looks like "C# String class doesn't implement .ToByte(), but the Powershell string class does, and provides it by way of IConvertible." So the leading nomenclature of the interface helps users see the difference in the C# and Powershell. –  Jul 06 '16 at 22:27