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

5
namespace System.Net;
6

7
using System.Collections;
8
using System.Collections.Generic;
9
using System.Numerics;
10

11
/// <summary>
12
/// Represents a collection of IP networks based on a given parent IP network and subnet CIDR.
13
/// </summary>
14
public class IPNetworkCollection : IEnumerable<IPNetwork2>, IEnumerator<IPNetwork2>
15
{
16
    private readonly byte cidrSubnet;
17
    private readonly IPNetwork2 ipnetwork;
18
    private BigInteger enumerator;
19

20
    private byte Cidr
21
    {
22
        get { return this.ipnetwork.Cidr; }
×
23
    }
24

25
    private BigInteger Broadcast
26
    {
27
        get { return IPNetwork2.ToBigInteger(this.ipnetwork.Broadcast!); }
×
28
    }
29

30
    private BigInteger LastUsable
31
    {
32
        get { return IPNetwork2.ToBigInteger(this.ipnetwork.LastUsable); }
×
33
    }
34

35
    private BigInteger Network
36
    {
37
        get { return IPNetwork2.ToBigInteger(this.ipnetwork.Network); }
×
38
    }
39

40
    /// <summary>
41
    /// Initializes a new instance of the <see cref="IPNetworkCollection"/> class.
42
    /// Represents a collection of IP networks based on a given parent IP network and subnet CIDR.
43
    /// </summary>
44
    /// <remarks>
45
    /// This class is used to generate a collection of IP networks by dividing the given parent IP network into subnets based on the provided subnet CIDR (Classless Inter-Domain Routing
46
    /// ) value.
47
    /// </remarks>
48
    /// <param name="ipnetwork">The network.</param>
49
    /// <param name="cidrSubnet">The subnet.</param>
50
    /// <exception cref="ArgumentOutOfRangeException">Cidr is out of range.</exception>
51
    /// <exception cref="ArgumentException">Network is invalid.</exception>
52
    internal IPNetworkCollection(IPNetwork2 ipnetwork, byte cidrSubnet)
×
53
    {
×
54
        int maxCidr = ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? 32 : 128;
×
55
        if (cidrSubnet > maxCidr)
×
56
        {
×
57
            throw new ArgumentOutOfRangeException(nameof(cidrSubnet));
×
58
        }
59

60
        if (cidrSubnet < ipnetwork.Cidr)
×
61
        {
×
62
            throw new ArgumentException("cidrSubnet < ipnetwork.Cidr", nameof(cidrSubnet));
×
63
        }
64

65
        this.cidrSubnet = cidrSubnet;
×
66
        this.ipnetwork = ipnetwork;
×
67
        this.enumerator = -1;
×
68
    }
×
69

70
    /// <summary>
71
    /// Gets the total number of IP addresses in the subnet.
72
    /// </summary>
73
    public BigInteger Count
74
    {
75
        get
76
        {
×
77
            var count = BigInteger.Pow(2, this.cidrSubnet - this.Cidr);
×
78
            return count;
×
79
        }
×
80
    }
81

82
    /// <summary>
83
    /// Retrieves an IPNetwork2 object from the collection by index.
84
    /// </summary>
85
    /// <param name="i">The index of the IPNetwork2 object to retrieve.</param>
86
    /// <returns>
87
    /// The IPNetwork2 object at the specified index.
88
    /// </returns>
89
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the specified index is greater than or equal to the Count of the collection.</exception>
90
    public IPNetwork2 this[BigInteger i]
91
    {
92
        get
93
        {
×
94
            if (i >= this.Count)
×
95
            {
×
96
                throw new ArgumentOutOfRangeException(nameof(i));
×
97
            }
98

99
            BigInteger last = this.ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetworkV6
×
100
                ? this.LastUsable
×
101
                : this.Broadcast;
×
102
            BigInteger increment = (last - this.Network) / this.Count;
×
103
            BigInteger uintNetwork = this.Network + ((increment + 1) * i);
×
104
            var ipn = new IPNetwork2(uintNetwork, this.ipnetwork.AddressFamily, this.cidrSubnet);
×
105
            return ipn;
×
106
        }
×
107
    }
108

109
    /// <inheritdoc/>
110
    IEnumerator<IPNetwork2> IEnumerable<IPNetwork2>.GetEnumerator()
111
    {
×
112
        return this;
×
113
    }
×
114

115
    /// <inheritdoc/>
116
    IEnumerator IEnumerable.GetEnumerator()
117
    {
×
118
        return this;
×
119
    }
×
120

121
    /// <inheritdoc/>
122
    public IPNetwork2 Current
123
    {
124
        get { return this[this.enumerator]; }
×
125
    }
126

127
    /// <summary>
128
    /// Releases all resources used by the object.
129
    /// </summary>
130
    /// <remarks>
131
    /// This method implements the IDisposable interface and releases any resources
132
    /// held by the object. In this particular implementation, there are no resources
133
    /// to dispose of, so the method does nothing.
134
    /// </remarks>
135
    public void Dispose()
136
    {
×
137
        Dispose(true);
×
138
        GC.SuppressFinalize(this);
×
139
    }
×
140

141
    /// <summary>
142
    /// Dispose instance 
143
    /// </summary>
144
    /// <param name="disposing"></param>
145
    protected virtual void Dispose(bool disposing)
146
    {
×
147
        // Cleanup
148
    }
×
149

150
    /// <summary>
151
    /// Gets the element in the collection at the current position of the enumerator.
152
    /// </summary>
153
    object IEnumerator.Current
154
    {
155
        get { return this.Current; }
×
156
    }
157

158
    /// <summary>
159
    /// Moves the enumerator to the next element in the collection.
160
    /// </summary>
161
    /// <returns>
162
    /// <see langword="true"/> if the enumerator was successfully moved to the next element;
163
    /// <see langword="false"/> if the enumerator has reached the end of the collection.
164
    /// </returns>
165
    public bool MoveNext()
166
    {
×
167
        this.enumerator++;
×
168
        return this.enumerator < this.Count;
×
169
    }
×
170

171
    /// <summary>
172
    /// Sets the enumerator to its initial position, which is before the first element in the collection.
173
    /// </summary>
174
    public void Reset()
175
    {
×
176
        this.enumerator = -1;
×
177
    }
×
178
}
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