IPNetwork Class
public class IPNetwork : System.Object, System.IComparable, System.IEquatable<IPNetwork>
Represents an IP network, typically defined by an IP address and a subnet mask or CIDR notation. This class provides methods for network calculations and comparisons.
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
- address: System.Net.IPAddress
The IP address to use for the network. - cidr: int
The CIDR prefix length (e.g., 24 for a /24 network).
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
- address: System.Net.IPAddress
The IP address to use for the network. - mask: System.Net.IPAddress
The subnet mask for the network.
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
- cidr: string
The IP network represented as a string in CIDR notation.
Method Details
Contains(IPAddress)
public bool Contains(System.Net.IPAddress address)
Determines whether the specified IP address is within the IP network.
Parameters
- address: System.Net.IPAddress
The IP address to check.
Returns
- bool:
true
if the IP address is within the network; otherwise,false
.
GetSubnet(int)
public IPNetwork GetSubnet(int cidr)
Creates a new IP network representing a subnet of the current network.
Parameters
- cidr: int
The CIDR prefix length for the new subnet.
Returns
- IPNetwork: A new
IPNetwork
object representing the subnet.
ToString()
public override string ToString()
Returns the string representation of the IP network in CIDR notation.
Returns
- string: The IP network in CIDR notation (e.g., "192.168.1.0/24").
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
- int: A hash code representing the IP network.
Equals(object)
public override bool Equals(object obj)
Determines whether the specified object is equal to the current object.
Parameters
- obj: object
The object to compare with the current object.
Returns
- bool:
true
if the specified object is equal to the current object; otherwise,false
.
Equals(IPNetwork)
public bool Equals(IPNetwork other)
Determines whether the specified IP network is equal to the current IP network.
Parameters
- other: IPNetwork
The IP network to compare with the current IP network.
Returns
- bool:
true
if the specified IP network is equal to the current IP network; otherwise,false
.
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
- left: IPNetwork
The left-hand operand. - right: IPNetwork
The right-hand operand.
Returns
- bool:
true
if the networks are equal; otherwise,false
.
!= (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
- left: IPNetwork
The left-hand operand. - right: IPNetwork
The right-hand operand.
Returns
- bool:
true
if the networks are not equal; otherwise,false
.
< (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
- left: IPNetwork
The left-hand operand. - right: IPNetwork
The right-hand operand.
Returns
- bool:
true
if the left network is less than the right network; otherwise,false
.
> (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
- left: IPNetwork
The left-hand operand. - right: IPNetwork
The right-hand operand.
Returns
- bool:
true
if the left network is greater than the right network; otherwise,false
.