PgInet
Namespace: Ankus
Assembly: Ankus.Runtime.dll
An immutable PostgreSQL inet address and prefix, preserving IPv4 and IPv6 identity. The default value is 0.0.0.0/0. Constructors and address operations work outside PostgreSQL.
[JsonConverter(typeof(PgInetConverter))]public readonly struct PgInet : IComparable<PgInet>, ISpanFormattable, IFormattable, IParsable<PgInet>, IEquatable<PgInet>Implements: IComparable<PgInet>, ISpanFormattable, IFormattable, IParsable<PgInet>, IEquatable<PgInet>
Constructors
Section titled “Constructors”PgInet(IPAddress, int?)
Section titled “PgInet(IPAddress, int?)”Copies an IP address, using its full width when no prefix is supplied. Scoped IPv6 addresses are rejected.
public PgInet(IPAddress address, int? prefixLength = null)Parameters:
address — IPAddress
The IPv4 or IPv6 host address.
prefixLength — int?
The prefix length, or null for a single host.
Properties
Section titled “Properties”Address
Section titled “Address”Gets a detached IPAddress copy of the host, without its network prefix.
public IPAddress Address { get; }Value: IPAddress
AddressFamily
Section titled “AddressFamily”Gets the preserved IPv4 or IPv6 address family; mapped IPv6 remains IPv6.
public AddressFamily AddressFamily { get; }Value: AddressFamily
Broadcast
Section titled “Broadcast”Gets the address with every host bit set, retaining its prefix.
public PgInet Broadcast { get; }Value: PgInet
Hostmask
Section titled “Hostmask”Gets the full-width host mask address.
public PgInet Hostmask { get; }Value: PgInet
Netmask
Section titled “Netmask”Gets the full-width network mask address.
public PgInet Netmask { get; }Value: PgInet
Network
Section titled “Network”Gets the network with host bits cleared, like PostgreSQL network(inet).
public PgCidr Network { get; }Value: PgCidr
PrefixLength
Section titled “PrefixLength”Gets the number of significant network bits.
public int PrefixLength { get; }Value: int
Methods
Section titled “Methods”CompareTo(PgInet)
Section titled “CompareTo(PgInet)”Compares family, common network bits, prefix length, then host bits, matching PostgreSQL inet ordering.
public int CompareTo(PgInet other)Parameters:
other — PgInet
The value to compare.
Returns: int
A negative, zero or positive comparison result.
Contains(PgInet, bool)
Section titled “Contains(PgInet, bool)”Tests subnet containment, including equal prefixes by default, like PostgreSQL >>=.
public bool Contains(PgInet other, bool includeEqual = true)Parameters:
other — PgInet
The address and subnet to compare.
includeEqual — bool
Whether equal prefix lengths count as contained.
Returns: bool
Whether the other subnet is contained within this one.
Equals(PgInet)
Section titled “Equals(PgInet)”public bool Equals(PgInet other)Parameters:
other — PgInet
Returns: bool
Equals(object)
Section titled “Equals(object)”public override bool Equals(object obj)Parameters:
obj — object
Returns: bool
GetHashCode()
Section titled “GetHashCode()”public override int GetHashCode()Returns: int
Parse(string)
Section titled “Parse(string)”Parses PostgreSQL inet input on the active backend, including PostgreSQL’s abbreviated IPv4 forms.
public static PgInet Parse(string text)Parameters:
text — string
The PostgreSQL address text.
Returns: PgInet
The parsed address and prefix.
ToIPAddress()
Section titled “ToIPAddress()”Returns an IPAddress only when discarding the prefix would lose no network information. Use Address to deliberately extract the host from a subnet value.
public IPAddress ToIPAddress()Returns: IPAddress
The detached, full-width host address.
ToString()
Section titled “ToString()”Formats a detached address, omitting a full-width prefix. No backend access is required.
public override string ToString()Returns: string
Round-trippable IPv4 or IPv6 text.
ToString(string?, IFormatProvider?)
Section titled “ToString(string?, IFormatProvider?)”Formats the value as PostgreSQL’s canonical text, the same text as ToString().
public string ToString(string? format, IFormatProvider? formatProvider)Parameters:
format — string
Null or empty; PostgreSQL text has one canonical form.
formatProvider — IFormatProvider
Ignored: the text does not depend on culture.
Returns: string
The canonical text.
Exceptions:
- FormatException: A format is specified.
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Section titled “TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)”Copies the value’s canonical PostgreSQL text into a span, as interpolated strings and other span-based formatters request it.
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)Parameters:
destination — Span<char>
The span that receives the text.
charsWritten — int
The number of characters written.
format — ReadOnlySpan<char>
Empty; PostgreSQL text has one canonical form.
provider — IFormatProvider
Ignored: the text does not depend on culture.
Returns: bool
Whether the text fit in the destination.
Exceptions:
- FormatException: A format is specified.
TryParse(string?, out PgInet)
Section titled “TryParse(string?, out PgInet)”Tries PostgreSQL parsing; input errors return false while backend-access and operational errors propagate.
public static bool TryParse(string? text, out PgInet value)Parameters:
text — string
The address text.
value — PgInet
The parsed address, or default.
Returns: bool
Whether the input is valid.
Invalid input leaves no error pending: PostgreSQL 16 and later report it as a soft input error, and earlier majors roll back an internal subtransaction. Before PostgreSQL 16, transaction callbacks and parallel operations forbid that rollback; native errors there remain pending and throw PgException.
WithPrefixLength(int)
Section titled “WithPrefixLength(int)”Changes the prefix without clearing host bits, like set_masklen(inet, integer).
public PgInet WithPrefixLength(int prefixLength)Parameters:
prefixLength — int
The new prefix length.
Returns: PgInet
The address with its new prefix.
Operators
Section titled “Operators”operator ==(PgInet, PgInet)
Section titled “operator ==(PgInet, PgInet)”public static bool operator ==(PgInet left, PgInet right)Parameters:
left — PgInet
right — PgInet
Returns: bool
operator >(PgInet, PgInet)
Section titled “operator >(PgInet, PgInet)”Compares two inet values in reverse PostgreSQL order.
public static bool operator >(PgInet left, PgInet right)Parameters:
left — PgInet
right — PgInet
Returns: bool
operator >=(PgInet, PgInet)
Section titled “operator >=(PgInet, PgInet)”Compares two inet values in reverse PostgreSQL order, including equality.
public static bool operator >=(PgInet left, PgInet right)Parameters:
left — PgInet
right — PgInet
Returns: bool
operator !=(PgInet, PgInet)
Section titled “operator !=(PgInet, PgInet)”public static bool operator !=(PgInet left, PgInet right)Parameters:
left — PgInet
right — PgInet
Returns: bool
operator <(PgInet, PgInet)
Section titled “operator <(PgInet, PgInet)”Compares two inet values in PostgreSQL order.
public static bool operator <(PgInet left, PgInet right)Parameters:
left — PgInet
right — PgInet
Returns: bool
operator <=(PgInet, PgInet)
Section titled “operator <=(PgInet, PgInet)”Compares two inet values in PostgreSQL order, including equality.
public static bool operator <=(PgInet left, PgInet right)Parameters:
left — PgInet
right — PgInet
Returns: bool