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

lduchosal / ipnetwork / 22803352265

07 Mar 2026 05:07PM UTC coverage: 91.848% (-1.3%) from 93.118%
22803352265

push

travis-pro

web-flow
Merge pull request #383 from lduchosal/feat/v4-nullable

feat/v4-nullable

651 of 721 branches covered (90.29%)

Branch coverage included in aggregate %.

111 of 147 new or added lines in 26 files covered. (75.51%)

5 existing lines in 3 files now uncovered.

1929 of 2088 relevant lines covered (92.39%)

574342.5 hits per line

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

93.18
/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
    {
33✔
25
        bool cidrified = InternalToCidr(false, netmask, out byte cidr);
33✔
26
        if (!cidrified)
30!
27
        {
×
NEW
28
            throw new ArgumentException("Invalid netmask.", nameof(netmask));
×
29
        }
30
        return cidr;
30✔
31
    }
30✔
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
    {
4,001,180✔
44
        return InternalToCidr(true, netmask, out cidr);
4,001,180✔
45
    }
4,001,180✔
46

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

56
            cidr = 0;
1✔
57
            return false;
1✔
58
        }
59

60
        TryToBigInteger(netmask, out BigInteger uintNetmask2);
4,001,211✔
61
        bool cidrified = InternalToCidr(tryParse, uintNetmask2, netmask.AddressFamily, out cidr);
4,001,211✔
62
        return cidrified;
4,001,209✔
63
    }
4,001,210✔
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
    {
4,001,211✔
77
        if (!InternalValidNetmask(netmask, family))
4,001,211✔
78
        {
7✔
79
            if (!tryParse)
7✔
80
            {
2✔
81
                throw new ArgumentException("Invalid netmask.", nameof(netmask));
2✔
82
            }
83

84
            cidr = 0;
5✔
85
            return false;
5✔
86
        }
87

88
        byte cidr2 = BitsSet(netmask);
4,001,204✔
89
        cidr = cidr2;
4,001,204✔
90
        return true;
4,001,204✔
91
    }
4,001,209✔
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