/// <summary> /// Takes a subnetmask (eg, "255.255.254.0") and returns the CIDR bit length of that /// address. Throws an exception if the passed address is not valid as a subnet mask. /// </summary> /// <param name="subnetMask">The subnet mask to use</param> /// <returns></returns> public static int SubnetMaskLength(IPAddress subnetMask) { var length = Bits.GetBitMaskLength(subnetMask.GetAddressBytes()); if (length == null) { throw new ArgumentException("Not a valid subnet mask", "subnetMask"); } return(length.Value); }