Padded Version.ToString() extension I want to convert a Version object to a string, but with the parts padded to a given width. E.g. 0.22.1 with parts padded to a width of 2 when converted to string would be "00.22.01"

A simple task, simple enough that I'm surprised it's not part of the class already. I'm not that used to .Net and it's string handling though, could my implementation be more efficient given that I might call it quite often? Or are there already built-in function(s) that might do this for me?

Option Strict On
Option Explicit On

Imports System.Runtime.CompilerServices
Imports System.Text

Module BetterVersion

    &lt;Extension()&gt; Public Function ToString(version As Version, fieldCount As Integer, fieldPadding As Integer) As String
        Dim Builder As StringBuilder = New StringBuilder()
        ' Convert the version to a string and split the result at the version 
        ' delimiter (i.e. ".")
        Dim Parts As String() = version.ToString(fieldCount).Split("."c)

        If fieldCount = 0 Then
            Return String.Empty ' No parts so no string
        ElseIf fieldCount = 1 Then
            ' Single part with padding and without trailing delimiter
            Builder.Append(Parts(0).PadLeft(fieldPadding, "0"c))
        Else ' fieldCount &gt;= 2
            For i As Integer = 0 To fieldCount - 2
                ' All but last part with padding and trailing delimiter
                Builder.AppendFormat("{0}.", Parts(i).PadLeft(fieldPadding, "0"c))
            Next
            ' Final part with padding and without trailing delimiter
            Builder.AppendFormat("{0}", Parts(fieldCount - 1).PadLeft(fieldPadding, "0"c))
        End If

        Return Builder.ToString()
    End Function
End Module
version.ToString(fieldCount) already returns the required amount of numbers. The first character of a variable is usually lower case. Parts contains the numbers in an array, you just need to modify that array and use the String.Join to get your number back. If you pass fieldCount of 0, you don't need to split anything. 

&lt;Extension()&gt; Public Function ToString(version As Version, fieldCount As Integer, fieldPadding As Integer) As String

    If fieldCount = 0 Then
        Return String.Empty
    End If

    Dim parts As String() = version.ToString(fieldCount).Split("."c)

    For i As Integer = 0 To parts.Count - 1
        parts(i) = parts(i).PadLeft(fieldPadding, "0"c)
    Next

    Return String.Join(".", parts)
End Function


An alternative would be to look at the code and do your own instead of splitting.