• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 960

15 May 2026 07:18AM UTC coverage: 0.0% (-94.1%) from 94.053%
960

Pull #411

appveyor

web-flow
Merge ef6b054ce into 4cec191ae
Pull Request #411: Bump MSTest.TestFramework from 4.2.2 to 4.2.3

0 of 2438 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/System.Net.IPNetwork/IPNetwork2ToCidr.cs
1
// <copyright file="IPNetwork2ToCidr.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Net.Sockets;
8
using System.Numerics;
9

10
/// <summary>
11
/// ToCidr.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Convert netmask to CIDR
17
    ///  255.255.255.0 -> 24
18
    ///  255.255.0.0   -> 16
19
    ///  255.0.0.0     -> 8.
20
    /// </summary>
21
    /// <param name="netmask">An IP Address representing the CIDR to convert.</param>
22
    /// <returns>A byte representing the CIDR converted from the netmask.</returns>
23
    public static byte ToCidr(IPAddress netmask)
24
    {
×
25
        bool cidrified = InternalToCidr(false, netmask, out byte cidr);
×
26
        if (!cidrified)
×
27
        {
×
28
            throw new ArgumentException("Invalid netmask.", nameof(netmask));
×
29
        }
30
        return cidr;
×
31
    }
×
32

33
    /// <summary>
34
    /// Convert netmask to CIDR
35
    ///  255.255.255.0 -> 24
36
    ///  255.255.0.0   -> 16
37
    ///  255.0.0.0     -> 8.
38
    /// </summary>
39
    /// <param name="netmask">An IPAddress representing the CIDR to convert.</param>
40
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
41
    /// <returns>true if netmask was converted successfully; otherwise, false.</returns>
42
    public static bool TryToCidr(IPAddress netmask, out byte cidr)
43
    {
×
44
        return InternalToCidr(true, netmask, out cidr);
×
45
    }
×
46

47
    private static bool InternalToCidr(bool tryParse, IPAddress netmask, out byte cidr)
48
    {
×
49
        if (netmask == null)
×
50
        {
×
51
            if (!tryParse)
×
52
            {
×
53
                throw new ArgumentNullException(nameof(netmask));
×
54
            }
55

56
            cidr = 0;
×
57
            return false;
×
58
        }
59

60
        TryToBigInteger(netmask, out BigInteger uintNetmask2);
×
61
        bool cidrified = InternalToCidr(tryParse, uintNetmask2, netmask.AddressFamily, out cidr);
×
62
        return cidrified;
×
63
    }
×
64

65
    /// <summary>
66
    /// Convert netmask to CIDR
67
    ///  255.255.255.0 -> 24
68
    ///  255.255.0.0   -> 16
69
    ///  255.0.0.0     -> 8.
70
    /// </summary>
71
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
72
    /// <param name="netmask">A number representing the netmask to convert.</param>
73
    /// <param name="family">Either IPv4 or IPv6.</param>
74
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
75
    private static bool InternalToCidr(bool tryParse, BigInteger netmask, AddressFamily family, out byte cidr)
76
    {
×
77
        if (!InternalValidNetmask(netmask, family))
×
78
        {
×
79
            if (!tryParse)
×
80
            {
×
81
                throw new ArgumentException("Invalid netmask.", nameof(netmask));
×
82
            }
83

84
            cidr = 0;
×
85
            return false;
×
86
        }
87

88
        byte cidr2 = BitsSet(netmask);
×
89
        cidr = cidr2;
×
90
        return true;
×
91
    }
×
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc