IPAddress.TryParse Method

Definition

Overloads

TryParse(String, IPAddress)

Determines whether a string is a valid IP address.

TryParse(ReadOnlySpan<Byte>, IPAddress)
TryParse(ReadOnlySpan<Char>, IPAddress)

Tries to parse a span of characters into a value.

TryParse(String, IPAddress)

Source:
IPAddress.cs
Source:
IPAddress.cs
Source:
IPAddress.cs

Determines whether a string is a valid IP address.

C#
public static bool TryParse(string ipString, out System.Net.IPAddress address);
C#
public static bool TryParse(string? ipString, out System.Net.IPAddress? address);

Parameters

ipString
String

The string to parse.

address
IPAddress

The IPAddress version of the string.

Returns

true if ipString was able to be parsed as an IP address; otherwise, false.

Remarks

The static TryParse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

The number of parts (each part is separated by a period) in ipString determines how the IP address is constructed. A one part address is stored directly in the network address. A two part address, convenient for specifying a class A address, puts the leading part in the first byte and the trailing part in the right-most three bytes of the network address. A three part address, convenient for specifying a class B address, puts the first part in the first byte, the second part in the second byte, and the final part in the right-most two bytes of the network address. For example:

Number of parts and example ipString IPv4 address for IPAddress
1 -- "65535" 0.0.255.255
2 -- "20.2" 20.0.0.2
2 -- "20.65535" 20.0.255.255
3 -- "128.1.2" 128.1.0.2
4 -- "1.1.1.10" 1.1.1.10
4 -- "1.1.1.010" 1.1.1.8
1 -- "0x2F" 0.0.0.47

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

TryParse(ReadOnlySpan<Byte>, IPAddress)

C#
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out System.Net.IPAddress? result);

Parameters

utf8Text
ReadOnlySpan<Byte>
result
IPAddress

Returns

Applies to

.NET 10
Product Versions
.NET 10

TryParse(ReadOnlySpan<Char>, IPAddress)

Source:
IPAddress.cs
Source:
IPAddress.cs
Source:
IPAddress.cs

Tries to parse a span of characters into a value.

C#
public static bool TryParse(ReadOnlySpan<char> ipSpan, out System.Net.IPAddress? address);
C#
public static bool TryParse(ReadOnlySpan<char> ipString, out System.Net.IPAddress address);

Parameters

ipStringipSpan
ReadOnlySpan<Char>

The byte span to parse.

address
IPAddress

When this method returns, the IPAddress version of the byte span.

Returns

true if ipSpan was able to be parsed as an IP address; otherwise, false.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1