Why does the System.Net.IpAddress
allow the following strings to be converted to valid IP addresses?
$b = [ipaddress]"10.10.10"
$b.IPAddressToString
#10.10.0.10
$c = [ipaddress]"10.10"
$c.IPAddressToString
#10.0.0.10
$d = [ipaddress]"10"
$d.IPAddressToString
#0.0.0.10
I can see that the pattern is that the last octet in the string is the last octet in the IPAddress
object, and whatever the first octets are in the string, are used as the left most octets in the IPAddress
, and zeros are used to fill the middle unspecified octets, if any.
But why does it do this? As a user I'd expect it to fail during conversion unless all octets are specified. Because it allows these conversions, unexpected results like this are possible when checking if a string is a valid IP address:
[bool]("10" -as [ipaddress]) #Outputs True