• 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/IPNetwork2Subnet.cs
1
// <copyright file="IPNetwork2Subnet.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Diagnostics.CodeAnalysis;
8

9
/// <summary>
10
/// Subnet.
11
/// </summary>
12
public sealed partial class IPNetwork2
13
{
14
    /// <summary>
15
    /// Subnet a network into multiple nets of cidr mask
16
    /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25
17
    /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9.
18
    /// </summary>
19
    /// <param name="cidr1">A byte representing the CIDR to be used to subnet the current IPNetwork.</param>
20
    /// <param name="ipnetworkCollection">The resulting subnetted IPNetwork.</param>
21
    /// <returns>true if network was split successfully; otherwise, false.</returns>
22
    public bool TrySubnet(byte cidr1, out IPNetworkCollection? ipnetworkCollection)
23
    {
×
24
        return InternalSubnet(true, this, cidr1, out ipnetworkCollection);
×
25
    }
×
26

27
    /// <summary>
28
    /// Subnet a network into multiple nets of cidr mask
29
    /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25
30
    /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9.
31
    /// </summary>
32
    /// <param name="cidr1">A byte representing the CIDR to be used to subnet the current IPNetwork.</param>
33
    /// <returns>A IPNetworkCollection split by CIDR.</returns>
34
    public IPNetworkCollection Subnet(byte cidr1)
35
    {
×
36
        if (!InternalSubnet(false, this, cidr1, out IPNetworkCollection? ipnetworkCollection))
×
37
        {
×
38
            throw new ArgumentException("Invalid CIDR.", nameof(cidr1));
×
39
        }
40

41
        return ipnetworkCollection;
×
42
    }
×
43

44
    /// <summary>
45
    /// Splits a given IP network into smaller subnets of the specified CIDR size.
46
    /// </summary>
47
    /// <param name="trySubnet">Indicates whether to throw exceptions or return null on failure.</param>
48
    /// <param name="network">The IP network to be subnetted.</param>
49
    /// <param name="cidr">The CIDR value used to define the new subnet size.</param>
50
    /// <param name="ipnetworkCollection">The resulting collection of subnets, or null if the operation fails and trySubnet is true.</param>
51
    internal static bool InternalSubnet(bool trySubnet, IPNetwork2 network, byte cidr, [NotNullWhen(true)] out IPNetworkCollection? ipnetworkCollection)
52
    {
×
53
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
54
        if (network == null)
×
55
        {
×
56
            if (!trySubnet)
×
57
            {
×
58
                throw new ArgumentNullException(nameof(network));
×
59
            }
60

61
            ipnetworkCollection = null;
×
62
            return false;
×
63
        }
64

65
        int maxCidr = network.family == Sockets.AddressFamily.InterNetwork ? 32 : 128;
×
66
        if (cidr > maxCidr)
×
67
        {
×
68
            if (!trySubnet)
×
69
            {
×
70
                throw new ArgumentOutOfRangeException(nameof(cidr));
×
71
            }
72

73
            ipnetworkCollection = null;
×
74
            return false;
×
75
        }
76

77
        if (cidr < network.Cidr)
×
78
        {
×
79
            if (!trySubnet)
×
80
            {
×
81
                throw new ArgumentException("CIDR is smaller than the network CIDR.", nameof(cidr));
×
82
            }
83

84
            ipnetworkCollection = null;
×
85
            return false;
×
86
        }
87

88
        ipnetworkCollection = new IPNetworkCollection(network, cidr);
×
89
        return true;
×
90
    }
×
91
}
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