Class NetworkCidr
Describes a IP network subnet using Classless Inter-Domain Routing (CIDR) notation.
Namespace: Neon.Net
Assembly: Neon.Common.dll
Syntax
public class NetworkCidr
Remarks
This is generally used for describing an IP subnet. See the following Wikipedia article for more information.
https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation
note
This class currently supports only IPv4 addresses.
Constructors
NetworkCidr(IPAddress, int)
Constructs a subnet from an IPv4 address and prefix length.
Declaration
public NetworkCidr(IPAddress address, int prefixLength)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | The IP address. |
int | prefixLength | The network prefix mask length in bits. |
NetworkCidr(IPAddress, IPAddress)
Constructs a subnet from an IPv4 address and subnet mask.
Declaration
public NetworkCidr(IPAddress address, IPAddress subnetMask)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | The IP address. |
IPAddress | subnetMask | The subnet mask. |
Properties
Address
Returns the CIDR address.
Declaration
public IPAddress Address { get; }
Property Value
Type | Description |
---|---|
IPAddress |
AddressCount
Returns the number of IP addresses within the subnet.
Declaration
public long AddressCount { get; }
Property Value
Type | Description |
---|---|
long |
All
Returns the 0.0.0.0/0 subnet which includes all public and private IP addresses.
Declaration
public static NetworkCidr All { get; }
Property Value
Type | Description |
---|---|
NetworkCidr |
FirstAddress
Returns the first IP address in the subnet.
Declaration
public IPAddress FirstAddress { get; }
Property Value
Type | Description |
---|---|
IPAddress |
FirstUsableAddress
Returns the first usable IP address in the subnet. This is often the gateway address.
Declaration
public IPAddress FirstUsableAddress { get; }
Property Value
Type | Description |
---|---|
IPAddress |
LastAddress
Returns the last IP address in the subnet.
Declaration
public IPAddress LastAddress { get; }
Property Value
Type | Description |
---|---|
IPAddress |
Mask
Returns the subnet mask.
Declaration
public IPAddress Mask { get; }
Property Value
Type | Description |
---|---|
IPAddress |
NextAddress
Returns the first address after the subnet.
Declaration
public IPAddress NextAddress { get; }
Property Value
Type | Description |
---|---|
IPAddress |
PrefixLength
Returns the prefix length in bits.
Declaration
public int PrefixLength { get; }
Property Value
Type | Description |
---|---|
int |
UsableAddressCount
Returns the number of usable IP addresses within the subnet. This returns
AddressCount - 1
to exclude the first address (.0) in the subnet as
well as the last address which is reserved for broadcasting.
Declaration
public long UsableAddressCount { get; }
Property Value
Type | Description |
---|---|
long |
Methods
Contains(NetworkCidr)
Determines whether a subnet is completely contained within the current subnet.
Declaration
public bool Contains(NetworkCidr subnet)
Parameters
Type | Name | Description |
---|---|---|
NetworkCidr | subnet | The subnet being tested. |
Returns
Type | Description |
---|---|
bool |
|
Contains(IPAddress)
Determines whether an IP address is within the subnet.
Declaration
public bool Contains(IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | The IP address. |
Returns
Type | Description |
---|---|
bool |
|
Exceptions
Type | Condition |
---|---|
NotSupportedException | Thrown if for IPv6 addresses. |
Equals(object)
Determines whether the specified object is equal to the current object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare with the current object. |
Returns
Type | Description |
---|---|
bool | true if the specified object is equal to the current object; otherwise, false. |
Overrides
GetHashCode()
Serves as the default hash function.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A hash code for the current object. |
Overrides
Normalize(string)
Attempts to normalize a network CIDR string by ensuring that the address actually fits the mask.
Declaration
public static string Normalize(string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The input string. |
Returns
Type | Description |
---|---|
string | The normalizes CIDR converted back to a string. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the input is not a valid CIDR. |
Overlaps(NetworkCidr)
Determines whether this subnet overlaps another.
Declaration
public bool Overlaps(NetworkCidr subnet)
Parameters
Type | Name | Description |
---|---|---|
NetworkCidr | subnet | The subnet being tested. |
Returns
Type | Description |
---|---|
bool |
|
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown if |
Parse(string)
Parses a subnet from CIDR notation in the form of ip-address/prefix, where prefix is the network prefix length in bits.
Declaration
public static NetworkCidr Parse(string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The input string. |
Returns
Type | Description |
---|---|
NetworkCidr | The parsed NetworkCidr. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the input is not correctly formatted. |
ToString()
Returns a string that represents the current object.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | A string that represents the current object. |
Overrides
TryParse(string, out NetworkCidr)
Attempts to parse a subnet from CIDR notation in the form of ip-address/prefix, where prefix is the network prefix length in bits.
Declaration
public static bool TryParse(string input, out NetworkCidr cidr)
Parameters
Type | Name | Description |
---|---|---|
string | input | The input string. |
NetworkCidr | cidr | The parsed NetworkCidr. |
Returns
Type | Description |
---|---|
bool |
|
Operators
operator ==(NetworkCidr, NetworkCidr)
Compares two NetworkCidr instances for equality.
Declaration
public static bool operator ==(NetworkCidr v1, NetworkCidr v2)
Parameters
Type | Name | Description |
---|---|---|
NetworkCidr | v1 | Value 1. |
NetworkCidr | v2 | Value 2 |
Returns
Type | Description |
---|---|
bool |
|
implicit operator string(NetworkCidr)
Implicitly casts a NetworkCidr into a string.
Declaration
public static implicit operator string(NetworkCidr v)
Parameters
Type | Name | Description |
---|---|---|
NetworkCidr | v | The value (or |
Returns
Type | Description |
---|---|
string |
operator !=(NetworkCidr, NetworkCidr)
Compares two NetworkCidr instances for inequality.
Declaration
public static bool operator !=(NetworkCidr v1, NetworkCidr v2)
Parameters
Type | Name | Description |
---|---|---|
NetworkCidr | v1 | Value 1. |
NetworkCidr | v2 | Value 2 |
Returns
Type | Description |
---|---|
bool |
|