Skip to content

PgCidr

Namespace: Ankus

Assembly: Ankus.Runtime.dll

An immutable PostgreSQL cidr network. Host bits must be zero; the default is 0.0.0.0/0.

[JsonConverter(typeof(PgCidrConverter))]
public readonly struct PgCidr : IComparable<PgCidr>, ISpanFormattable, IFormattable, IParsable<PgCidr>, IEquatable<PgCidr>

Implements: IComparable<PgCidr>, ISpanFormattable, IFormattable, IParsable<PgCidr>, IEquatable<PgCidr>

Creates a network from an inet value whose host bits are already zero. Use PgInet.Network to explicitly clear host bits first.

public PgCidr(PgInet value)

Parameters:

value — PgInet

The network value.

Creates a network without silently discarding host bits.

public PgCidr(IPAddress address, int prefixLength)

Parameters:

address — IPAddress

The network address.

prefixLength — int

The network prefix length.

Copies a .NET IPNetwork, rejecting scoped IPv6 addresses.

public PgCidr(IPNetwork network)

Parameters:

network — IPNetwork

The network to copy.

Gets a detached copy of the network address.

public IPAddress Address { get; }

Value: IPAddress

Gets this network as an inet value without information loss.

public PgInet Inet { get; }

Value: PgInet

Gets the number of network bits.

public int PrefixLength { get; }

Value: int

Compares using PostgreSQL network ordering.

public int CompareTo(PgCidr other)

Parameters:

other — PgCidr

The network to compare.

Returns: int

The comparison result.

public bool Equals(PgCidr other)

Parameters:

other — PgCidr

Returns: bool

public override bool Equals(object obj)

Parameters:

obj — object

Returns: bool

public override int GetHashCode()

Returns: int

Parses PostgreSQL cidr text on the active backend, enforcing zero host bits.

public static PgCidr Parse(string text)

Parameters:

text — string

The network text.

Returns: PgCidr

The parsed network.

Converts to a .NET IPNetwork without loss of address family or prefix.

public IPNetwork ToIPNetwork()

Returns: IPNetwork

The equivalent .NET network.

Formats a detached network with an explicit prefix, without backend access.

public override string ToString()

Returns: string

The round-trippable network 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 backend parsing without swallowing backend-access or operational failures.

public static bool TryParse(string? text, out PgCidr value)

Parameters:

text — string

The network text.

value — PgCidr

The parsed network, or default.

Returns: bool

Whether the text 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 and clears any host bits exposed by the change, like set_masklen(cidr, integer).

public PgCidr WithPrefixLength(int prefixLength)

Parameters:

prefixLength — int

The new prefix length.

Returns: PgCidr

The normalized network.

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

Parameters:

left — PgCidr

right — PgCidr

Returns: bool

Compares two networks in reverse PostgreSQL order.

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

Parameters:

left — PgCidr

right — PgCidr

Returns: bool

Compares two networks in reverse PostgreSQL order, including equality.

public static bool operator >=(PgCidr left, PgCidr right)

Parameters:

left — PgCidr

right — PgCidr

Returns: bool

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

Parameters:

left — PgCidr

right — PgCidr

Returns: bool

Compares two networks in PostgreSQL order.

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

Parameters:

left — PgCidr

right — PgCidr

Returns: bool

Compares two networks in PostgreSQL order, including equality.

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

Parameters:

left — PgCidr

right — PgCidr

Returns: bool