Skip to content

[API Proposal]: IPAddress.IsValid #111282

@MihaZupan

Description

@MihaZupan

Background and motivation

Cheaper IPAddress.TryParse(span, out _) for when you don't need the result.

API Proposal

namespace System.Net;

public partial class IPAddress
{
    public static IPAddress Parse(string ipString);
    public static IPAddress Parse(ReadOnlySpan<char> ipSpan);
    public static IPAddress Parse(ReadOnlySpan<byte> utf8Text);
    public static bool TryParse(string? ipString, out IPAddress? address);
    public static bool TryParse(ReadOnlySpan<char> ipSpan, out IPAddress? address);
    public static bool TryParse(ReadOnlySpan<byte> utf8Text, out IPAddress? result);

+   public static bool IsValid(ReadOnlySpan<char> ipSpan);
+   public static bool IsValid(ReadOnlySpan<byte> utf8Text);
}

API Usage

-if (IPAddress.TryParse(text, out _))
+if (IPAddress.IsValid(text))

We have an internal helper TargetHostNameHelper.IsValidAddress that appears to be doing exactly what IPAddress.IsValid would be:

internal static unsafe bool IsValidAddress(string? hostname)

Alternative Designs

  • Allow specifying IPv4 or IPv6 only?

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.Netin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions