Skip to content

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>

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.

Gets a detached IPAddress copy of the host, without its network prefix.

public IPAddress Address { get; }

Value: IPAddress

Gets the preserved IPv4 or IPv6 address family; mapped IPv6 remains IPv6.

public AddressFamily AddressFamily { get; }

Value: AddressFamily

Gets the address with every host bit set, retaining its prefix.

public PgInet Broadcast { get; }

Value: PgInet

Gets the full-width host mask address.

public PgInet Hostmask { get; }

Value: PgInet

Gets the full-width network mask address.

public PgInet Netmask { get; }

Value: PgInet

Gets the network with host bits cleared, like PostgreSQL network(inet).

public PgCidr Network { get; }

Value: PgCidr

Gets the number of significant network bits.

public int PrefixLength { get; }

Value: int

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.

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.

public bool Equals(PgInet other)

Parameters:

other — PgInet

Returns: bool

public override bool Equals(object obj)

Parameters:

obj — object

Returns: bool

public override int GetHashCode()

Returns: int

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.

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.

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.

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:

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:

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.

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.

public static bool operator ==(PgInet left, PgInet right)

Parameters:

left — PgInet

right — PgInet

Returns: bool

Compares two inet values in reverse PostgreSQL order.

public static bool operator >(PgInet left, PgInet right)

Parameters:

left — PgInet

right — PgInet

Returns: bool

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

public static bool operator !=(PgInet left, PgInet right)

Parameters:

left — PgInet

right — PgInet

Returns: bool

Compares two inet values in PostgreSQL order.

public static bool operator <(PgInet left, PgInet right)

Parameters:

left — PgInet

right — PgInet

Returns: bool

Compares two inet values in PostgreSQL order, including equality.

public static bool operator <=(PgInet left, PgInet right)

Parameters:

left — PgInet

right — PgInet

Returns: bool