Language: EN


How to Parse IP Ranges with C# IpaddressRange

IpAddressRange in .NET is an open-source library that makes working with IP address ranges easy.

It provides functions for parsing IPs in text string format, supporting a wide variety of formats.

Once an IP range object is defined, it provides comparison operations and range membership checking.

The library is compatible with both IPv4 and IPv6 versions of IP addresses and supports three different range formats: CIDR, subnet mask, and IP address range.

How to use IpAddressRange

We can easily add the library to a .NET project through the corresponding Nuget package.

Install-Package IPAddressRange

Here are some examples of how to use IpAddressRange taken from the library documentation

using NetTools;
// rangeA.Begin is "", and rangeA.End is "".
var rangeA = IPAddressRange.Parse("");
rangeA.Contains(IPAddress.Parse("")); // is True.
rangeA.Contains(IPAddress.Parse("")); // is False.
rangeA.ToCidrString(); // is

// rangeB.Begin is "", and rangeB.End is "".
var rangeB1 = IPAddressRange.Parse(" -");
rangeB1.Contains(IPAddress.Parse("")); // is True.
rangeB1.Contains(IPAddress.Parse("")); // is False.

// IEnumerable<IPAddress> support, it's lazy evaluation.
foreach (var ip in IPAddressRange.Parse(""))

IpAddressRange is open-source, and all the code and documentation is available in the project repository at