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 354263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:54:27+00:00 2026-05-12T11:54:27+00:00

How should I correctly store IP address list with addresses which are subnets to

  • 0

How should I correctly store IP address list with addresses which are subnets to make it searchable?

There are two examples:

  1. I have IP address 1.2.3.4 and in my C# List there is 1.2.3.4 entry so here we have no problems.

  2. I have IP address 3.4.5.6 and in my C# List I have subnet 3.4.0.0/24. Here is my problem.

How to store IP subnet in List to cover second example?

  • 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-05-12T11:54:28+00:00Added an answer on May 12, 2026 at 11:54 am

    At the end of this answer you will find a complete implementation of a structure to represent a IPV4 address.

    Here is really simple example of usage:-

    List<IPV4Address> list = new List<IPV4Address>();
    list.Add(IPV4Address.FromString("3.4.0.0", 24));
    var x = IPV4Address.FromString("3.4.0.6");
    foreach (var addr in list.Where(a => a.Contains(x)))
      Console.WriteLine(addr);
    

    The value “3.4.0.0/255.255.255.0” is displayed inthe console since 3.4.0.6 is found in the 3.4.0.0/24 subnet. Assuming list is full of various subnets and x could contain any address then this:-

    var result = list.Where(a => a.Contains(x))
        .OrderByDescending(a => a.Mask)
        .FirstOrDefault();
    

    will select the most specific subnet for that contains x.

    public struct IPV4Address
    {
      private UInt32 _Value;
      private UInt32 _Mask;
    
      public UInt32 Value
      {
        get { return _Value; }
        private set { _Value = value; }
      }
    
      public UInt32 Mask
      {
        get { return _Mask; }
        private set { _Mask = value; }
      }
    
      public static IPV4Address FromString(string address)
      {
        return FromString(address, 32);
      }
    
      public static IPV4Address FromString(string address, int maskLength)
      {
        string[] parts = address.Split('.');
        UInt32 value = ((UInt32.Parse(parts[0]) << 24) +
          ((UInt32.Parse(parts[1])) << 16) +
          ((UInt32.Parse(parts[2])) << 8) +
          UInt32.Parse(parts[3]));
    
        return new IPV4Address(value, maskLength);
      }
    
      public IPV4Address(UInt32 value)
      {
        _Value = value;
        _Mask = int.MaxValue;
      }
    
      public IPV4Address(UInt32 value, int maskLength)
      {
        if (maskLength < 0 || maskLength > 32)
          throw new ArgumentOutOfRangeException("maskLength", "Must be 0 to 32");
    
        _Value = value;
        if (maskLength == 32)
          _Mask = UInt32.MaxValue;
        else
          _Mask = ~(UInt32)((1 << (32 - maskLength))-1);
    
        if ((_Value & _Mask) != _Value)
          throw new ArgumentException("Address value must be contained in mask");
      }
    
      public bool Contains(IPV4Address address)
      {
        if ((Mask & address.Mask) == Mask)
        {
          return (address.Value & Mask) == Value;
        }
        return false;
      }
    
      public override string ToString()
      {
        string result = String.Format("{0}.{1}.{2}.{3}", (_Value >> 24), 
          (_Value >> 16) & 0xFF, 
          (_Value >> 8) & 0xFF, 
          _Value & 0xFF);
    
        if (_Mask != UInt32.MaxValue)
          result += "/" + String.Format("{0}.{1}.{2}.{3}", (_Mask >> 24),
          (_Mask >> 16) & 0xFF,
          (_Mask >> 8) & 0xFF,
          _Mask & 0xFF);
    
        return result;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.