@@ -83,16 +83,18 @@ public static T IsGreaterThanOrEqualTo<T>(T value, T comparand, string paramName
83
83
}
84
84
85
85
/// <summary>
86
- /// Ensures that the value of a parameter is greater than or equal to zero .
86
+ /// Ensures that the value of a parameter is greater than a comparand .
87
87
/// </summary>
88
+ /// <typeparam name="T">Type type of the value.</typeparam>
88
89
/// <param name="value">The value of the parameter.</param>
90
+ /// <param name="comparand">The comparand.</param>
89
91
/// <param name="paramName">The name of the parameter.</param>
90
92
/// <returns>The value of the parameter.</returns>
91
- public static int IsGreaterThanOrEqualToZero ( int value , string paramName )
93
+ public static T IsGreaterThan < T > ( T value , T comparand , string paramName ) where T : IComparable < T >
92
94
{
93
- if ( value < 0 )
95
+ if ( value . CompareTo ( comparand ) <= 0 )
94
96
{
95
- var message = string . Format ( "Value is not greater than or equal to 0 : {0 }." , value ) ;
97
+ var message = $ "Value is not greater than { comparand } : { value } .";
96
98
throw new ArgumentOutOfRangeException ( paramName , message ) ;
97
99
}
98
100
return value ;
@@ -104,79 +106,62 @@ public static int IsGreaterThanOrEqualToZero(int value, string paramName)
104
106
/// <param name="value">The value of the parameter.</param>
105
107
/// <param name="paramName">The name of the parameter.</param>
106
108
/// <returns>The value of the parameter.</returns>
107
- public static long IsGreaterThanOrEqualToZero ( long value , string paramName )
108
- {
109
- if ( value < 0 )
110
- {
111
- var message = string . Format ( "Value is not greater than or equal to 0: {0}." , value ) ;
112
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
113
- }
114
- return value ;
115
- }
109
+ public static int IsGreaterThanOrEqualToZero ( int value , string paramName ) =>
110
+ IsGreaterThanOrEqualTo ( value , 0 , paramName ) ;
116
111
117
112
/// <summary>
118
113
/// Ensures that the value of a parameter is greater than or equal to zero.
119
114
/// </summary>
120
115
/// <param name="value">The value of the parameter.</param>
121
116
/// <param name="paramName">The name of the parameter.</param>
122
117
/// <returns>The value of the parameter.</returns>
123
- public static TimeSpan IsGreaterThanOrEqualToZero ( TimeSpan value , string paramName )
124
- {
125
- if ( value < TimeSpan . Zero )
126
- {
127
- var message = string . Format ( "Value is not greater than or equal to zero: {0}." , TimeSpanParser . ToString ( value ) ) ;
128
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
129
- }
130
- return value ;
131
- }
118
+ public static long IsGreaterThanOrEqualToZero ( long value , string paramName ) =>
119
+ IsGreaterThanOrEqualTo ( value , 0 , paramName ) ;
120
+
121
+ /// <summary>
122
+ /// Ensures that the value of a parameter is greater than or equal to zero.
123
+ /// </summary>
124
+ /// <param name="value">The value of the parameter.</param>
125
+ /// <param name="paramName">The name of the parameter.</param>
126
+ /// <returns>The value of the parameter.</returns>
127
+ public static TimeSpan IsGreaterThanOrEqualToZero ( TimeSpan value , string paramName ) =>
128
+ IsGreaterThanOrEqualTo ( value , TimeSpan . Zero , paramName ) ;
132
129
133
130
/// <summary>
134
131
/// Ensures that the value of a parameter is greater than zero.
135
132
/// </summary>
136
133
/// <param name="value">The value of the parameter.</param>
137
134
/// <param name="paramName">The name of the parameter.</param>
138
135
/// <returns>The value of the parameter.</returns>
139
- public static int IsGreaterThanZero ( int value , string paramName )
140
- {
141
- if ( value <= 0 )
142
- {
143
- var message = string . Format ( "Value is not greater than zero: {0}." , value ) ;
144
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
145
- }
146
- return value ;
147
- }
136
+ public static int IsGreaterThanZero ( int value , string paramName ) =>
137
+ IsGreaterThan ( value , 0 , paramName ) ;
148
138
149
139
/// <summary>
150
140
/// Ensures that the value of a parameter is greater than zero.
151
141
/// </summary>
152
142
/// <param name="value">The value of the parameter.</param>
153
143
/// <param name="paramName">The name of the parameter.</param>
154
144
/// <returns>The value of the parameter.</returns>
155
- public static long IsGreaterThanZero ( long value , string paramName )
156
- {
157
- if ( value <= 0 )
158
- {
159
- var message = string . Format ( "Value is not greater than zero: {0}." , value ) ;
160
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
161
- }
162
- return value ;
163
- }
145
+ public static long IsGreaterThanZero ( long value , string paramName ) =>
146
+ IsGreaterThan ( value , 0 , paramName ) ;
164
147
165
148
/// <summary>
166
149
/// Ensures that the value of a parameter is greater than zero.
167
150
/// </summary>
168
151
/// <param name="value">The value of the parameter.</param>
169
152
/// <param name="paramName">The name of the parameter.</param>
170
153
/// <returns>The value of the parameter.</returns>
171
- public static TimeSpan IsGreaterThanZero ( TimeSpan value , string paramName )
172
- {
173
- if ( value <= TimeSpan . Zero )
174
- {
175
- var message = string . Format ( "Value is not greater than zero: {0}." , value ) ;
176
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
177
- }
178
- return value ;
179
- }
154
+ public static double IsGreaterThanZero ( double value , string paramName ) =>
155
+ IsGreaterThan ( value , 0 , paramName ) ;
156
+
157
+ /// <summary>
158
+ /// Ensures that the value of a parameter is greater than zero.
159
+ /// </summary>
160
+ /// <param name="value">The value of the parameter.</param>
161
+ /// <param name="paramName">The name of the parameter.</param>
162
+ /// <returns>The value of the parameter.</returns>
163
+ public static TimeSpan IsGreaterThanZero ( TimeSpan value , string paramName ) =>
164
+ IsGreaterThan ( value , TimeSpan . Zero , paramName ) ;
180
165
181
166
/// <summary>
182
167
/// Ensures that the value of a parameter is infinite or greater than or equal to zero.
@@ -298,6 +283,24 @@ public static T IsNull<T>(T value, string paramName) where T : class
298
283
return value ;
299
284
}
300
285
286
+ /// <summary>
287
+ /// Ensures that the value of a parameter is null or is between a minimum and a maximum value.
288
+ /// </summary>
289
+ /// <typeparam name="T">Type type of the value.</typeparam>
290
+ /// <param name="value">The value of the parameter.</param>
291
+ /// <param name="min">The minimum value.</param>
292
+ /// <param name="max">The maximum value.</param>
293
+ /// <param name="paramName">The name of the parameter.</param>
294
+ /// <returns>The value of the parameter.</returns>
295
+ public static T ? IsNullOrBetween < T > ( T ? value , T min , T max , string paramName ) where T : struct , IComparable < T >
296
+ {
297
+ if ( value != null )
298
+ {
299
+ IsBetween ( value . Value , min , max , paramName ) ;
300
+ }
301
+ return value ;
302
+ }
303
+
301
304
/// <summary>
302
305
/// Ensures that the value of a parameter is null or greater than or equal to zero.
303
306
/// </summary>
0 commit comments