Class IPAddressRange
Represents an inclusive range of IP addresses.
Namespace: Swan.Net
Syntax
public sealed class IPAddressRange : IEquatable<IPAddressRange>
Remarks
This class makes no distinction between IPv4 addresses and the same addresses mapped to IPv6 for the purpose of determining whether it belongs to a range: that is, the Contains(IPAddress) method of an instance initialized with IPv4 addresses, or with the same addresses mapped to IPv6, will return true for both an in-range IPv4 address and the same address mapped to IPv6.
The IPAddressRange(IPAddress, IPAddress) constructor, however, does make such distinction: you cannot initialize a range using an IPv4 address and an IPv6 address, even if the latter is an IPv4 address mapped to IPv6, nor the other way around.
Constructors
IPAddressRange(IPAddress)
Initializes a new instance of the IPAddressRange class, representing a single IP address.
Declaration
public IPAddressRange(IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | The IP address. |
IPAddressRange(IPAddress, IPAddress)
Initializes a new instance of the IPAddressRange class,
representing a range of IP addresses between start
and end
, extremes included.
Declaration
public IPAddressRange(IPAddress start, IPAddress end)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | start | The starting address of the range. |
IPAddress | end | The ending address of the range. |
IPAddressRange(IPAddress, Byte)
Initializes a new instance of the IPAddressRange class, representing a CIDR subnet.
Declaration
public IPAddressRange(IPAddress baseAddress, byte prefixLength)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | baseAddress | The base address of the subnet. |
Byte | prefixLength | The prefix length of the subnet. |
Fields
All
Gets an instance of IPAddressRange that contains all possible IP addresses.
The Contains(IPAddress) method of the returned instance will always return true.
Declaration
public static readonly IPAddressRange All
Field Value
Type | Description |
---|---|
IPAddressRange |
AllIPv4
Gets an instance of IPAddressRange that contains all IPv4 addresses.
The Contains(IPAddress) method of the returned instance will return true for all IPv4 addresses, as well as their IPv6 mapped counterparts, and false for all other IPv6 addresses.
Declaration
public static readonly IPAddressRange AllIPv4
Field Value
Type | Description |
---|---|
IPAddressRange |
None
Gets an instance of IPAddressRange that contains no addresses.
The Contains(IPAddress) method of the returned instance will always return false.
This property is useful to initialize non-nullable properties of type IPAddressRange.
Declaration
public static readonly IPAddressRange None
Field Value
Type | Description |
---|---|
IPAddressRange |
Properties
AddressFamily
Gets the address family of the IP address range.
Declaration
public AddressFamily AddressFamily { get; }
Property Value
Type | Description |
---|---|
AddressFamily |
Remarks
Regardless of the value of this property, IPv4 addresses and their IPv6 mapped counterparts will be considered the same for the purposes of the Contains(IPAddress) method.
End
Gets an instance of
Declaration
public IPAddress End { get; }
Property Value
Type | Description |
---|---|
IPAddress |
IsSubnet
Gets a value indicating whether this instance represents a CIDR subnet.
Declaration
public bool IsSubnet { get; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
This property is true only for instances initialized via the IPAddressRange(IPAddress, Byte) constructor. Instances constructed by specifying a range will have this property set to false even when they actually represent a subnet.
For example, the instance returned by IPAddressRange.Parse("192.168.0.0-192.168.0.255")
will have this property set to false; for this property to be true,
the string passed to Parse(String) should instead be "192.168.0.0/24"
(a CIDR subnet specification) or "192.168.0.0/255.255.255.0" (a base address / netmask pair,
only accepted by Parse(String) and TryParse(String, out IPAddressRange) for IPv4 addresses.)
Start
Gets an instance of
Declaration
public IPAddress Start { get; }
Property Value
Type | Description |
---|---|
IPAddress |
Methods
Contains(IPAddress)
Determines whether the given address
sa contained in this range.
Declaration
public bool Contains(IPAddress address)
Parameters
Type | Name | Description |
---|---|---|
IPAddress | address | The IP address to check. |
Returns
Type | Description |
---|---|
Boolean | true if |
Remarks
This method treats IPv4 addresses and their IPv6-mapped counterparts
the same; that is, given a range obtained by parsing the string 192.168.1.0/24
,
Contains(IPAddress.Parse("192.168.1.55"))
will return true,
as will Contains(IPAddress.Parse("192.168.1.55").MapToIPv6())
. This is true
as well if a range is initialized with IPv6 addresses.
Equals(IPAddressRange)
Declaration
public bool Equals(IPAddressRange other)
Parameters
Type | Name | Description |
---|---|---|
IPAddressRange | other |
Returns
Type | Description |
---|---|
Boolean |
Equals(Object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj |
Returns
Type | Description |
---|---|
Boolean |
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
Parse(String)
Converts the string representation of a range of IP addresses to an instance of IPAddressRange.
Declaration
public static IPAddressRange Parse(string str)
Parameters
Type | Name | Description |
---|---|---|
String | str | The string to convert. |
Returns
Type | Description |
---|---|
IPAddressRange | An instance of IPAddressRange representing the same range of
IP addresses represented by |
Remarks
This method supports the following formats for str
:
FormatDescriptionExamples | |
---|---|
Single addressA single IP address.
| |
Range of addressesStart and end address, separated by a hyphen (- ).
| |
CIDR subnetBase address and prefix length, separated by a slash (/ ).
| |
"Legacy" subnet
Base address and netmask, separated by a slash ( Only accepted for IPv4 addresses.
|
See Also
ToString()
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String |
Remarks
The result of this method will be a string that, if passed to the Parse(String) or TryParse(String, out IPAddressRange) method, will result in an instance identical to this one.
If this instance has been created by means of the Parse(String) or TryParse(String, out IPAddressRange) method, the returned string will not necessarily be identical to the parsed string. The possible differences include the following:
TryParse(String, out IPAddressRange)
Tries to convert the string representation of a range of IP addresses to an instance of IPAddressRange.
Declaration
public static bool TryParse(string str, out IPAddressRange result)
Parameters
Type | Name | Description |
---|---|---|
String | str | The string to convert. |
IPAddressRange | result | When this method returns true,
an instance of IPAddressRange representing the same range of
IP addresses represented by |
Returns
Type | Description |
---|---|
Boolean | true if the conversion was successful; otherwise, false. |
Remarks
See the "Remarks" section of Parse(String)
for an overview of the formats accepted for str
.