There is a class for checking IP addresses System.Net.IPAddress the static method has been around since the .NET Framework 2.0 TryParse()which extracts an IP address from a string.
Read more after the ad

Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which, with 53 renowned experts, supports numerous medium-sized and large companies through consulting and training as well as with software development. Through his appearances at numerous national and international conferences as well as more than 90 specialist books and more than 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.
Since .NET Core 2.1 the extraction is also from the type ReadOnlySpan possible. The return value is a boolvalue and the extracted IP address is in the form of an instance of the class IPAddress as out-Parameters supplied. If you just want to check whether the IP address is correct, you write IPAddress.TryParse(eingabe, out _).
In .NET 10.0, Microsoft now provides in the static method IsValid() another test variant with less internal effort. The following code compares IsValid() with TryParse():
///
/// IsValid mit ReadOnlySpan oder ReadOnlySpan als Alternative zu IPAddress.TryParse(span, out _)
///
public void Run()
{
CUI.Demo("IPAddress.IsValid()");
string IP1 = "192.168.1.0"; // gültige IPv4-Adresse
CUI.H2(IP1);
// --- Alt
var valid1 = IPAddress.TryParse(IP1, out _);
Console.WriteLine("TryParse: " + valid1); // true
// --- Neu
System.Console.WriteLine("IsValid: " + System.Net.IPAddress.IsValid(IP1)); // true
string IP2 = "192.168.256.1"; // ungültige IPv4-Adresse
CUI.H2(IP2);
// --- Alt
var valid2 = IPAddress.TryParse(IP2, out _);
Console.WriteLine("TryParse: " + valid2); // false
// --- Neu
System.Console.WriteLine("IsValid: " + System.Net.IPAddress.IsValid(IP2)); // false
}

The code uses the old and new methods to check IP addresses for validity (Fig. 1).
To do this, Microsoft is now simply returning a previously internal method TargetHostNameHelper.IsValidAddress() to the outside world, as can be seen from the issue on GitHub.
Read more after the ad

IsValid() is more performant than TryParse() and requires no memory (Fig. 2).
(Bild: Microsoft)
(Image: King / stock.adobe.com)

This is new in .NET 11.0: Dr. Holger Schwichtenberg and other experts will present the changes for developers at the betterCode() .NET 11.0 online conference on November 17, 2026. Discounted blind bird tickets are available until the program is published.
(rme)
