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

lduchosal / ipnetwork / 738

29 Mar 2025 10:31PM UTC coverage: 93.524% (+0.2%) from 93.279%
738

push

appveyor

web-flow
feat/code-quality (#350)

* Chore: file scoped namespace

* Feat: sign assembly
Fix: documentation on public methodes

* Fix: documentation and warnings

* Chore: split IPNetwork2 into multiple partial classes

* Chore: split IPNetwork2

* Chore: document partial classes

* Fix: order methodes

* Chore: documentd

* Chore: cleanup

1603 of 1714 relevant lines covered (93.52%)

825934.22 hits per line

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

92.21
/src/System.Net.IPNetwork/IPAddressCollection.cs
1
// <copyright file="IPAddressCollection.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 different filters for a collection of items.
13
/// </summary>
14
public enum FilterEnum
15
{
16
    /// <summary>
17
    /// Every IPAdresses are returned
18
    /// </summary>
19
    All,
20

21
    /// <summary>
22
    /// Returns only usable IPAdresses
23
    /// </summary>
24
    Usable,
25
}
26

27
/// <summary>
28
/// Represents a collection of IP addresses within a specific IP network.
29
/// </summary>
30
public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
31
{
32
    private readonly IPNetwork2 ipnetwork;
33
    private readonly FilterEnum filter;
34
    private BigInteger enumerator;
35

36
    /// <summary>
37
    /// Initializes a new instance of the <see cref="IPAddressCollection"/> class.
38
    /// </summary>
39
    /// <param name="ipnetwork">The network.</param>
40
    /// <param name="filter">The filter.</param>
41
    internal IPAddressCollection(IPNetwork2 ipnetwork, FilterEnum filter)
32✔
42
    {
32✔
43
        this.ipnetwork = ipnetwork;
32✔
44
        this.filter = filter;
32✔
45
        this.Reset();
32✔
46
    }
32✔
47

48
    #region Count, Array, Enumerator
49

50
    /// <summary>
51
    /// Gets the count of IP addresses within the network.
52
    /// </summary>
53
    /// <value>
54
    /// The count of IP addresses within the network.
55
    /// </value>
56
    public BigInteger Count
57
    {
58
        get
59
        {
266✔
60
            BigInteger count = this.ipnetwork.Total;
266✔
61
            if (this.filter == FilterEnum.Usable)
266✔
62
            {
27✔
63
                count -= 2;
27✔
64
            }
27✔
65

66
            if (count < 0)
266✔
67
            {
1✔
68
                count = 0;
1✔
69
            }
1✔
70

71
            return count;
266✔
72
        }
266✔
73
    }
74

75
    /// <summary>
76
    /// Gets the IP address corresponding to the given index from the IPNetwork collection.
77
    /// </summary>
78
    /// <param name="i">The index of the IP address to retrieve.</param>
79
    /// <returns>The IP address corresponding to the given index.</returns>
80
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the given index is greater than or equal to the Count property of the IPNetwork collection.</exception>
81
    public IPAddress this[BigInteger i]
82
    {
83
        get
84
        {
132✔
85
            if (i >= this.Count)
132✔
86
            {
6✔
87
                throw new ArgumentOutOfRangeException("i");
6✔
88
            }
89

90
            byte width = this.ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128;
126✔
91
            IPNetworkCollection ipn = this.ipnetwork.Subnet(width);
126✔
92

93
            BigInteger index = i;
126✔
94
            if (this.filter == FilterEnum.Usable)
126✔
95
            {
18✔
96
                index++;
18✔
97
            }
18✔
98

99
            return ipn[index].Network;
126✔
100
        }
126✔
101
    }
102

103
    #endregion
104

105
    #region Legacy Enumeration
106

107
    /// <summary>
108
    /// Gets the current <see cref="IPAddress"/> from the collection.
109
    /// </summary>
110
    /// <returns>The current <see cref="IPAddress"/>.</returns>
111
    public IPAddress Current
112
    {
113
        get
114
        {
64✔
115
            return this[this.enumerator];
64✔
116
        }
60✔
117
    }
118

119
    /// <inheritdoc/>
120
    object IEnumerator.Current
121
    {
122
        get
123
        {
14✔
124
            return this.Current;
14✔
125
        }
12✔
126
    }
127

128
    /// <inheritdoc />
129
    public bool MoveNext()
130
    {
104✔
131
        this.enumerator++;
104✔
132
        if (this.enumerator >= this.Count)
104✔
133
        {
20✔
134
            return false;
20✔
135
        }
136

137
        return true;
84✔
138
    }
104✔
139

140
    /// <inheritdoc />
141
    public void Reset()
142
    {
42✔
143
        this.enumerator = -1;
42✔
144
    }
42✔
145

146
    /// <inheritdoc />
147
    public void Dispose()
148
    {
25✔
149
        // nothing to dispose
150
    }
25✔
151

152
    #endregion
153

154
    #region Enumeration
155

156
    /// <inheritdoc/>
157
    IEnumerator<IPAddress> IEnumerable<IPAddress>.GetEnumerator()
158
    {
3✔
159
        return new Enumerator(this);
3✔
160
    }
3✔
161

162
    /// <inheritdoc/>
163
    IEnumerator IEnumerable.GetEnumerator()
164
    {
1✔
165
        return new Enumerator(this);
1✔
166
    }
1✔
167

168
    private struct Enumerator : IEnumerator<IPAddress>
169
    {
170
        private readonly IPAddressCollection collection;
171
        private BigInteger enumerator;
172

173
        object IEnumerator.Current
174
        {
175
            get
176
            {
×
177
                return this.Current;
×
178
            }
×
179
        }
180

181
        public IPAddress Current
182
        {
183
            get
184
            {
22✔
185
                return this.collection[this.enumerator];
22✔
186
            }
22✔
187
        }
188

189
        public void Dispose()
190
        {
3✔
191
            // nothing to dispose
192
        }
3✔
193

194
        public bool MoveNext()
195
        {
25✔
196
            this.enumerator++;
25✔
197
            if (this.enumerator >= this.collection.Count)
25✔
198
            {
3✔
199
                return false;
3✔
200
            }
201

202
            return true;
22✔
203
        }
25✔
204

205
        public void Reset()
206
        {
×
207
            this.enumerator = -1;
×
208
        }
×
209

210
        public Enumerator(IPAddressCollection collection)
211
        {
4✔
212
            this.collection = collection;
4✔
213
            this.enumerator = -1;
4✔
214
        }
4✔
215
    }
216

217
    #endregion
218
}
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