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

lduchosal / ipnetwork / 809

17 Aug 2025 08:25AM UTC coverage: 93.223% (-1.0%) from 94.226%
809

push

appveyor

web-flow
Chore: cleanup, breaking changes, enum, tryparse, exception, static ListIPAddress (#363)

* Chore: huge cleanup, enum, tryparse, exception, static ListIPAddress, important changes : IPNetwork comparison and sort order have change to reflect expected behavoir
* Fix: obsolete enums
* Fix: network sorting and member comparison
* Chore: upgrade version number 3.3

1802 of 1933 relevant lines covered (93.22%)

726934.37 hits per line

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

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

5
namespace System.Net;
6

7
/// <summary>
8
/// Regroup the tryParse functionalities.
9
/// </summary>
10
public partial class IPNetwork2
11
{
12
    /// <summary>
13
    /// 192.168.168.100 - 255.255.255.0
14
    ///
15
    /// Network   : 192.168.168.0
16
    /// Netmask   : 255.255.255.0
17
    /// Cidr      : 24
18
    /// Start     : 192.168.168.1
19
    /// End       : 192.168.168.254
20
    /// Broadcast : 192.168.168.255.
21
    /// </summary>
22
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
23
    /// <param name="netmask">A string containing a netmask to convert (255.255.255.0).</param>
24
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
25
    /// <returns>true if ipaddress/netmask was converted successfully; otherwise, false.</returns>
26
    public static bool TryParse(string ipaddress, string netmask, out IPNetwork2 ipnetwork)
27
    {
20✔
28
        InternalParse(true, ipaddress, netmask, out IPNetwork2 ipnetwork2);
20✔
29
        bool parsed = ipnetwork2 != null;
20✔
30
        ipnetwork = ipnetwork2;
20✔
31

32
        return parsed;
20✔
33
    }
20✔
34

35
    /// <summary>
36
    /// 192.168.168.100/24
37
    ///
38
    /// Network   : 192.168.168.0
39
    /// Netmask   : 255.255.255.0
40
    /// Cidr      : 24
41
    /// Start     : 192.168.168.1
42
    /// End       : 192.168.168.254
43
    /// Broadcast : 192.168.168.255.
44
    /// </summary>
45
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
46
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
47
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
48
    /// <returns>true if ipaddress/cidr was converted successfully; otherwise, false.</returns>
49
    public static bool TryParse(string ipaddress, byte cidr, out IPNetwork2 ipnetwork)
50
    {
68✔
51
        InternalParse(true, ipaddress, cidr, out IPNetwork2 ipnetwork2);
68✔
52
        bool parsed = ipnetwork2 != null;
68✔
53
        ipnetwork = ipnetwork2;
68✔
54

55
        return parsed;
68✔
56
    }
68✔
57

58
    /// <summary>
59
    /// 192.168.0.1/24
60
    /// 192.168.0.1 255.255.255.0
61
    ///
62
    /// Network   : 192.168.0.0
63
    /// Netmask   : 255.255.255.0
64
    /// Cidr      : 24
65
    /// Start     : 192.168.0.1
66
    /// End       : 192.168.0.254
67
    /// Broadcast : 192.168.0.255.
68
    /// </summary>
69
    /// <param name="network">A string containing an ip network to convert.</param>
70
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
71
    /// <returns>true if network was converted successfully; otherwise, false.</returns>
72
    public static bool TryParse(string network, out IPNetwork2 ipnetwork)
73
    {
46✔
74
        bool parsed = InternalParse(true, network, CidrGuess.ClassFull, sanitize: true, out ipnetwork);
46✔
75
        return parsed;
46✔
76
    }
46✔
77

78
    /// <summary>
79
    /// 192.168.0.1/24
80
    /// 192.168.0.1 255.255.255.0
81
    ///
82
    /// Network   : 192.168.0.0
83
    /// Netmask   : 255.255.255.0
84
    /// Cidr      : 24
85
    /// Start     : 192.168.0.1
86
    /// End       : 192.168.0.254
87
    /// Broadcast : 192.168.0.255.
88
    /// </summary>
89
    /// <param name="network">A string containing an ip network to convert.</param>
90
    /// <param name="sanitanize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
91
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
92
    /// <returns>true if network was converted successfully; otherwise, false.</returns>
93
    public static bool TryParse(string network, bool sanitanize, out IPNetwork2 ipnetwork)
94
    {
14✔
95
        bool parsed = InternalParse(true, network, CidrGuess.ClassFull, sanitanize, out ipnetwork);
14✔
96
        return parsed;
14✔
97
    }
14✔
98

99
    /// <summary>
100
    /// 192.168.0.1/24
101
    /// 192.168.0.1 255.255.255.0
102
    ///
103
    /// Network   : 192.168.0.0
104
    /// Netmask   : 255.255.255.0
105
    /// Cidr      : 24
106
    /// Start     : 192.168.0.1
107
    /// End       : 192.168.0.254
108
    /// Broadcast : 192.168.0.255.
109
    /// </summary>
110
    /// <param name="ipaddress">An IPAddress to convert.</param>
111
    /// <param name="netmask">An IPAddress to be used as netmask to convert.</param>
112
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
113
    /// <returns>true if network was converted successfully; otherwise, false.</returns>
114
    public static bool TryParse(IPAddress ipaddress, IPAddress netmask, out IPNetwork2 ipnetwork)
115
    {
4✔
116
        bool parsed = InternalParse(true, ipaddress, netmask, out ipnetwork);
4✔
117
        return parsed;
4✔
118
    }
4✔
119

120
    /// <summary>
121
    /// 192.168.0.1/24
122
    /// 192.168.0.1 255.255.255.0
123
    ///
124
    /// Network   : 192.168.0.0
125
    /// Netmask   : 255.255.255.0
126
    /// Cidr      : 24
127
    /// Start     : 192.168.0.1
128
    /// End       : 192.168.0.254
129
    /// Broadcast : 192.168.0.255.
130
    /// </summary>
131
    /// <param name="network">A string containing an ip network to convert.</param>
132
    /// <param name="cidrGuess">A ICidrGuess implementation that will be used to guess CIDR during conversion.</param>
133
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
134
    /// <returns>true if network was converted successfully; otherwise, false.</returns>
135
    public static bool TryParse(string network, ICidrGuess cidrGuess, out IPNetwork2 ipnetwork)
136
    {
3✔
137
        bool parsed = InternalParse(true, network, cidrGuess, true, out ipnetwork);
3✔
138
        return parsed;
3✔
139
    }
3✔
140

141
    /// <summary>
142
    /// 192.168.0.1/24
143
    /// 192.168.0.1 255.255.255.0
144
    ///
145
    /// Network   : 192.168.0.0
146
    /// Netmask   : 255.255.255.0
147
    /// Cidr      : 24
148
    /// Start     : 192.168.0.1
149
    /// End       : 192.168.0.254
150
    /// Broadcast : 192.168.0.255.
151
    /// </summary>
152
    /// <param name="network">A string containing an ip network to convert.</param>
153
    /// <param name="cidrGuess">A ICidrGuess implementation that will be used to guess CIDR during conversion.</param>
154
    /// <param name="sanitanize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
155
    /// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
156
    /// <returns>true if network was converted successfully; otherwise, false.</returns>
157
    public static bool TryParse(string network, ICidrGuess cidrGuess, bool sanitanize, out IPNetwork2 ipnetwork)
158
    {
×
159
        bool parsed = InternalParse(true, network, cidrGuess, sanitanize, out ipnetwork);
×
160
        return parsed;
×
161
    }
×
162
}
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

© 2025 Coveralls, Inc