By non-instance types I mean types which do not expose a public constructor, for lack of a better term.
I want to extend the BitConverter class with an overload of ToString() that takes a parameter of type Char, representing a value delimiter.
Why? By default, the ToString() call returns a string representation of a byte array, delimited by dash symbols. The signature does not allow you to specify a different delimiter, which I find very unfortunate.
Now because this is not an instance type, or maybe because I’m overloading a shared method, I’m having a hard time finding the proper syntax to define my extension method.
What am I doing wrong here, causing the overloads to not show up in IntelliSense:
Imports System.Runtime.CompilerServices
Module BitConverterExtensions
<Extension()>
Public Function ToString(ByVal converter As BitConverter, ByVal value() As Byte, ByVal delimiter As Char) As String
Return BitConverterExtensions.ToString(converter, value, 0, value.Length, delimiter)
End Function
<Extension()>
Public Function ToString(ByVal converter As BitConverter, ByVal value() As Byte, ByVal startIndex As Integer, ByVal delimiter As Char) As String
Return BitConverterExtensions.ToString(converter, value, startIndex, value.Length, delimiter)
End Function
<Extension()>
Public Function ToString(ByVal converter As BitConverter, ByVal value() As Byte, ByVal startIndex As Integer, ByVal length As Integer, ByVal delimiter As Char) As String
Dim bytes As String = BitConverter.ToString(value, startIndex, length)
Return bytes.Replace("-"c, delimiter)
End Function
End Module
Or is it simply not possible to extend shared methods?
It’s not possible to introduce an overload of a method on a
shared / statictype. Extension methods are feature whose usage is driven by instances of a type not the type itself.