Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:28:37+00:00 2026-06-03T18:28:37+00:00

If I have an IP address range (CIDR notation) and I need to know

  • 0

If I have an IP address range (CIDR notation) and I need to know if some arbitrary IP address is within that range — both presented as strings — what is the easiest way to do this with C#?

Examples:

  • IPv4 Range: 192.168.168.100/24, IP to check: 192.168.168.200
  • IPv6 Range: fe80::202:b3ff:fe1e:8329/24, IP to check: 2001:db8::
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:28:38+00:00Added an answer on June 3, 2026 at 6:28 pm

    Here’s a simple class:

    public class IPSubnet
    {
        private readonly byte[] _address;
        private readonly int _prefixLength;
    
        public IPSubnet(string value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
    
            string[] parts = value.Split('/');
            if (parts.Length != 2)
                throw new ArgumentException("Invalid CIDR notation.", "value");
    
            _address = IPAddress.Parse(parts[0]).GetAddressBytes();
            _prefixLength = Convert.ToInt32(parts[1], 10);
        }
    
        public bool Contains(string address)
        {
            return this.Contains(IPAddress.Parse(address).GetAddressBytes());
        }
    
        public bool Contains(byte[] address)
        {
            if (address == null)
                throw new ArgumentNullException("address");
    
            if (address.Length != _address.Length)
                return false; // IPv4/IPv6 mismatch
    
            int index = 0;
            int bits = _prefixLength;
    
            for (; bits >= 8; bits -= 8)
            {
                if (address[index] != _address[index])
                    return false;
                ++index;
            }
    
            if (bits > 0)
            {
                int mask = (byte)~(255 >> bits);
                if ((address[index] & mask) != (_address[index] & mask))
                    return false;
            }
    
            return true;
        }
    }
    

    Sample usage:

    Console.WriteLine(new IPSubnet("192.168.168.100/24").Contains("192.168.168.200")); // True
    Console.WriteLine(new IPSubnet("fe80::202:b3ff:fe1e:8329/24").Contains("2001:db8::")); // False
    

    This class treats all IPv4 addresses as distinct from all IPv6 addresses, making no attempt to translate between IPv4 and IPv6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Range variable that contains an address - $2:$2,$4:$205,$214:$214 - (3 groups
If I have an IP address: 192.100.100.2 and need to ensure that it falls
Currently I have an address column that is an embedded document in my users
I have a database with Points of Interest that all have an address. I
In an embedded application, we have a table describing the various address ranges that
I have 6 devices configured on IP address 1 to 255 in the range
I have a user submitted IP address with netmask, and I need to make
The DOM specification on Range objects doesn't address whether a range can have an
I have a C-function that allocates memory at the address passed to and is
I have a line of code that keeps crashing NSArray* address = [NSArray arrayWithArray:[[[access.filteredResults

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.