IPAddress.Parse Method

Definition

Overloads

Parse(ReadOnlySpan<Char>)

Converts an IP address represented as a character span to an IPAddress instance.

Parse(String)

Converts an IP address string to an IPAddress instance.

Parse(ReadOnlySpan<Byte>)

Parse(ReadOnlySpan<Char>)

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

Converts an IP address represented as a character span to an IPAddress instance.

C#
public static System.Net.IPAddress Parse(ReadOnlySpan<char> ipSpan);
C#
public static System.Net.IPAddress Parse(ReadOnlySpan<char> ipString);

Parameters

ipStringipSpan
ReadOnlySpan<Char>

A character span that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

Returns

The converted IP address.

Exceptions

ipString is not a valid IP address.

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

Parse(String)

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

Converts an IP address string to an IPAddress instance.

C#
public static System.Net.IPAddress Parse(string ipString);

Parameters

ipString
String

A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.

Returns

An IPAddress instance.

Exceptions

ipString is null.

ipString is not a valid IP address.

Examples

The following code converts a string that contains an IP address, in dotted-quad notation for IPv4 or in colon-hexadecimal notation for IPv6, into an instance of the IPAddress class. Then it uses the overloaded ToString method to display the address in standard notation.

C#

using System;
using System.Net;

class ParseAddress
{

  private static void Main(string[] args)
  {
    string IPaddress;

    if (args.Length == 0)
    {
      Console.WriteLine("Please enter an IP address.");
      Console.WriteLine("Usage:   >cs_parse any IPv4 or IPv6 address.");
      Console.WriteLine("Example: >cs_parse 127.0.0.1");
      Console.WriteLine("Example: >cs_parse 0:0:0:0:0:0:0:1");
      return;
    }
    else
        {
            IPaddress = args[0];
        }

        // Get the list of the IPv6 addresses associated with the requested host.
        Parse(IPaddress);
  }

  // This method calls the IPAddress.Parse method to check the ipAddress
  // input string. If the ipAddress argument represents a syntatically correct IPv4 or
  // IPv6 address, the method displays the Parse output into quad-notation or
  // colon-hexadecimal notation, respectively. Otherwise, it displays an
  // error message.
  private static void Parse(string ipAddress)
  {
    try
    {
      // Create an instance of IPAddress for the specified address string (in
      // dotted-quad, or colon-hexadecimal notation).
      IPAddress address = IPAddress.Parse(ipAddress);

      // Display the address in standard notation.
      Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());
    }

    catch(ArgumentNullException e)
    {
      Console.WriteLine("ArgumentNullException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

    catch(FormatException e)
    {
      Console.WriteLine("FormatException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

    catch(Exception e)
    {
      Console.WriteLine("Exception caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
   }
}

Remarks

The static Parse 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 1.1, 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

Parse(ReadOnlySpan<Byte>)

C#
public static System.Net.IPAddress Parse(ReadOnlySpan<byte> utf8Text);

Parameters

utf8Text
ReadOnlySpan<Byte>

Returns

Applies to

.NET 10
Product Versions
.NET 10