Summary

The IPNetwork class is a fundamental component in .NET for working with IP addressing and subnetting. It encapsulates an IP address and its associated network mask, enabling operations such as determining the network address, broadcast address, host address range, and performing network comparisons.

Properties

Name Type Description
Cidr int Gets the CIDR notation prefix length of the IP network.
CidrString string Gets the IP network represented as a string in CIDR notation (e.g., "192.168.1.0/24").
Address System.Net.IPAddress Gets the IP address associated with this network.
Mask System.Net.IPAddress Gets the subnet mask for this IP network.
NetworkAddress System.Net.IPAddress Gets the network address of this IP network.
BroadcastAddress System.Net.IPAddress Gets the broadcast address of this IP network.
FirstUsableAddress System.Net.IPAddress Gets the first usable IP address within the network.
LastUsableAddress System.Net.IPAddress Gets the last usable IP address within the network.
IsIPv4 bool Gets a value indicating whether the IP address in this network is an IPv4 address.
IsIPv6 bool Gets a value indicating whether the IP address in this network is an IPv6 address.

Constructors

Name Description
IPNetwork(IPAddress, int) Initializes a new instance of the IPNetwork class with the specified IP address and CIDR prefix length.
IPNetwork(IPAddress, IPAddress) Initializes a new instance of the IPNetwork class with the specified IP address and subnet mask.
IPNetwork(string) Initializes a new instance of the IPNetwork class from a string representation (e.g., "192.168.1.0/24").

Methods

Name Description
Contains(IPAddress) Determines whether the specified IP address is within the IP network.
GetSubnet(int) Creates a new IP network representing a subnet of the current network.
ToString() Returns the string representation of the IP network in CIDR notation.
GetHashCode() Returns the hash code for the current IP network.
Equals(object) Determines whether the specified object is equal to the current object.
Equals(IPNetwork) Determines whether the specified IP network is equal to the current IP network.

Operators

Name Description
== (IPNetwork, IPNetwork) Compares two IP networks for equality.
!= (IPNetwork, IPNetwork) Compares two IP networks for inequality.
< (IPNetwork, IPNetwork) Compares two IP networks to determine if the first is less than the second.
> (IPNetwork, IPNetwork) Compares two IP networks to determine if the first is greater than the second.

Examples

The following examples demonstrate common uses of the IPNetwork class.
// Create an IPNetwork from an IP address and CIDR prefix var network1 = new IPNetwork(IPAddress.Parse("192.168.1.0"), 24); Console.WriteLine(network1); // Output: 192.168.1.0/24 // Create an IPNetwork from an IP address and subnet mask var network2 = new IPNetwork(IPAddress.Parse("10.0.0.1"), IPAddress.Parse("255.255.0.0")); Console.WriteLine(network2.CidrString); // Output: 10.0.0.0/16 // Check if an IP address is contained within a network bool isContained = network1.Contains(IPAddress.Parse("192.168.1.100")); Console.WriteLine($"Is 192.168.1.100 in {network1}? {isContained}"); // Output: True // Get the network address and broadcast address Console.WriteLine($"Network Address: {network1.NetworkAddress}"); // Output: 192.168.1.0 Console.WriteLine($"Broadcast Address: {network1.BroadcastAddress}"); // Output: 192.168.1.255 // Get usable host addresses Console.WriteLine($"First Usable Address: {network1.FirstUsableAddress}"); // Output: 192.168.1.1 Console.WriteLine($"Last Usable Address: {network1.LastUsableAddress}"); // Output: 192.168.1.254 // Create a subnet var subnet = network1.GetSubnet(25); Console.WriteLine($"Subnet: {subnet}"); // Output: 192.168.1.0/25

Constructor Details

IPNetwork(IPAddress, int)

public IPNetwork(System.Net.IPAddress address, int cidr)
Initializes a new instance of the IPNetwork class with the specified IP address and CIDR prefix length.

Parameters

IPNetwork(IPAddress, IPAddress)

public IPNetwork(System.Net.IPAddress address, System.Net.IPAddress mask)
Initializes a new instance of the IPNetwork class with the specified IP address and subnet mask.

Parameters

IPNetwork(string)

public IPNetwork(string cidr)
Initializes a new instance of the IPNetwork class from a string representation (e.g., "192.168.1.0/24").

Parameters

Method Details

Contains(IPAddress)

public bool Contains(System.Net.IPAddress address)
Determines whether the specified IP address is within the IP network.

Parameters

Returns

GetSubnet(int)

public IPNetwork GetSubnet(int cidr)
Creates a new IP network representing a subnet of the current network.

Parameters

Returns

ToString()

public override string ToString()
Returns the string representation of the IP network in CIDR notation.

Returns

GetHashCode()

public override int GetHashCode()
Returns the hash code for the current IP network. This is useful for collections like dictionaries and hash sets.

Returns

Equals(object)

public override bool Equals(object obj)
Determines whether the specified object is equal to the current object.

Parameters

Returns

Equals(IPNetwork)

public bool Equals(IPNetwork other)
Determines whether the specified IP network is equal to the current IP network.

Parameters

Returns

Operator Details

== (IPNetwork, IPNetwork)

public static bool operator ==(IPNetwork left, IPNetwork right)
Compares two IP networks for equality. Returns true if both networks represent the same IP address range and CIDR prefix.

Parameters

Returns

!= (IPNetwork, IPNetwork)

public static bool operator !=(IPNetwork left, IPNetwork right)
Compares two IP networks for inequality. Returns true if the networks do not represent the same IP address range and CIDR prefix.

Parameters

Returns

< (IPNetwork, IPNetwork)

public static bool operator <(IPNetwork left, IPNetwork right)
Compares two IP networks to determine if the first is less than the second. This comparison is based on the numerical value of the IP addresses and CIDR prefixes.

Parameters

Returns

> (IPNetwork, IPNetwork)

public static bool operator >(IPNetwork left, IPNetwork right)
Compares two IP networks to determine if the first is greater than the second. This comparison is based on the numerical value of the IP addresses and CIDR prefixes.

Parameters

Returns