Created
October 13, 2023 09:31
-
-
Save SibeeshVenu/cd88ae1dcc383a6f1596e4a8eef270da to your computer and use it in GitHub Desktop.
IPNetwork Contains Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Determine whether a given The <see cref="IPAddress"/> is part of the IP network. | |
/// </summary> | |
/// <param name="address">The <see cref="IPAddress"/>.</param> | |
/// <returns><see langword="true"/> if the <see cref="IPAddress"/> is part of the IP network. Otherwise, <see langword="false"/>.</returns> | |
public bool Contains(IPAddress address) | |
{ | |
if (Prefix.AddressFamily != address.AddressFamily) | |
{ | |
return false; | |
} | |
var addressBytes = address.GetAddressBytes(); | |
for (int i = 0; i < PrefixBytes.Length && Mask[i] != 0; i++) | |
{ | |
if ((PrefixBytes[i] & Mask[i]) != (addressBytes[i] & Mask[i])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment