IPNetwork

Namespace: System.Net

Represents a network address and a subnet mask, encapsulating an IP network. This class provides functionality to work with IPv4 and IPv6 networks, including parsing, validation, and manipulation of network properties.

Constructors

IPNetwork(string cidr)

public IPNetwork(string cidr)

Initializes a new instance of the IPNetwork class using the specified CIDR (Classless Inter-Domain Routing) notation.

Parameters

  • cidr: A string representing the IP network in CIDR notation (e.g., "192.168.1.0/24" or "2001:db8::/32").

Example

// For IPv4 var ipv4Network = new IPNetwork("192.168.1.0/24"); // For IPv6 var ipv6Network = new IPNetwork("2001:db8::/32");

IPNetwork(IPAddress network, int prefixLength)

public IPNetwork(IPAddress network, int prefixLength)

Initializes a new instance of the IPNetwork class using the specified IP address and prefix length.

Parameters

  • network: The IP address representing the network address.
  • prefixLength: The length of the subnet mask (0-32 for IPv4, 0-128 for IPv6).

Example

using System.Net; var ipAddr = IPAddress.Parse("10.0.0.0"); var network = new IPNetwork(ipAddr, 8);

Properties

Network

public IPAddress Network { get; }

Gets the IP address that represents the network address.

Example

var ipNetwork = new IPNetwork("192.168.1.0/24"); Console.WriteLine(ipNetwork.Network); // Output: 192.168.1.0

Netmask

public IPAddress Netmask { get; }

Gets the IP address that represents the subnet mask.

Example

var ipNetwork = new IPNetwork("192.168.1.0/24"); Console.WriteLine(ipNetwork.Netmask); // Output: 255.255.255.0

PrefixLength

public int PrefixLength { get; }

Gets the prefix length of the IP network.

Example

var ipNetwork = new IPNetwork("192.168.1.0/24"); Console.WriteLine(ipNetwork.PrefixLength); // Output: 24

IsIPv4

public bool IsIPv4 { get; }

Gets a value indicating whether the IP network is an IPv4 network.

IsIPv6

public bool IsIPv6 { get; }

Gets a value indicating whether the IP network is an IPv6 network.

Broadcast

public IPAddress Broadcast { get; }

Gets the broadcast address of the IP network. This property is only applicable to IPv4 networks.

Example

var ipNetwork = new IPNetwork("192.168.1.0/24"); Console.WriteLine(ipNetwork.Broadcast); // Output: 192.168.1.255

FirstUsableAddress

public IPAddress FirstUsableAddress { get; }

Gets the first usable IP address in the network. For IPv4, this is the network address plus one. For IPv6, this is the network address.

LastUsableAddress

public IPAddress LastUsableAddress { get; }

Gets the last usable IP address in the network. For IPv4, this is the broadcast address minus one. For IPv6, this is the broadcast address.

AddressFamily

public System.Net.Sockets.AddressFamily AddressFamily { get; }

Gets the address family of the IP network (e.g., InterNetwork for IPv4, InterNetworkV6 for IPv6).

Methods

Contains(IPAddress address)

public bool Contains(IPAddress address)

Determines whether the specified IP address belongs to this network.

Parameters

  • address: The IP address to check.

Example

var network = new IPNetwork("192.168.1.0/24"); var ipToCheck = IPAddress.Parse("192.168.1.100"); bool isInNetwork = network.Contains(ipToCheck); // True

ToString()

public override string ToString()

Returns the string representation of the IP network in CIDR notation.

Example

var ipNetwork = new IPNetwork("10.0.0.0/8"); Console.WriteLine(ipNetwork.ToString()); // Output: 10.0.0.0/8

Parse(string cidr)

public static IPNetwork Parse(string cidr)

Parses a string representation of an IP network in CIDR notation and returns an IPNetwork object.

Parameters

  • cidr: The string to parse.

Returns

  • An IPNetwork object representing the parsed network.

Example

var network = IPNetwork.Parse("172.16.0.0/16");

TryParse(string cidr, out IPNetwork network)

public static bool TryParse(string cidr, out IPNetwork network)

Attempts to parse a string representation of an IP network in CIDR notation and returns a value that indicates whether the parsing succeeded.

Parameters

  • cidr: The string to parse.
  • network: When this method returns, contains the parsed IPNetwork object, if the parsing succeeded, or null if the parsing failed.

Returns

  • true if the cidr string was parsed successfully; otherwise, false.

Example

IPNetwork parsedNetwork; if (IPNetwork.TryParse("192.168.0.0/23", out parsedNetwork)) { // Parsing succeeded } else { // Parsing failed }

Operators

== (Equality)

public static bool operator ==(IPNetwork left, IPNetwork right)

Compares two IPNetwork objects for equality.

Parameters

  • left: The first IPNetwork object to compare.
  • right: The second IPNetwork object to compare.

Returns

  • true if the two IP networks are equal; otherwise, false.

!= (Inequality)

public static bool operator !=(IPNetwork left, IPNetwork right)

Compares two IPNetwork objects for inequality.

Parameters

  • left: The first IPNetwork object to compare.
  • right: The second IPNetwork object to compare.

Returns

  • true if the two IP networks are not equal; otherwise, false.