Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 0fd20a2

Browse files
driesengjkotalik
authored andcommitted
Use Nullable<T>.GetValueOrDefault() instead of Nullable<T>.Value when a it is known to have a value. (#1063)
1 parent cb10388 commit 0fd20a2

File tree

17 files changed

+53
-54
lines changed

17 files changed

+53
-54
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public virtual CookieOptions Build(HttpContext context, DateTimeOffset expiresFr
107107
Domain = Domain,
108108
IsEssential = IsEssential,
109109
Secure = SecurePolicy == CookieSecurePolicy.Always || (SecurePolicy == CookieSecurePolicy.SameAsRequest && context.Request.IsHttps),
110-
Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.Value) : default(DateTimeOffset?)
110+
Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.GetValueOrDefault()) : default(DateTimeOffset?)
111111
};
112112
}
113113
}

src/Microsoft.AspNetCore.Http.Abstractions/Internal/ParsingHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ public static void SetHeaderUnmodified(IHeaderDictionary headers, string key, St
103103
{
104104
throw new ArgumentNullException(nameof(key));
105105
}
106-
if (!values.HasValue || StringValues.IsNullOrEmpty(values.Value))
106+
if (!values.HasValue || StringValues.IsNullOrEmpty(values.GetValueOrDefault()))
107107
{
108108
headers.Remove(key);
109109
}
110110
else
111111
{
112-
headers[key] = values.Value;
112+
headers[key] = values.GetValueOrDefault();
113113
}
114114
}
115115

src/Microsoft.AspNetCore.Http.Extensions/HeaderDictionaryTypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ internal static void SetDate(this IHeaderDictionary headers, string name, DateTi
138138

139139
if (value.HasValue)
140140
{
141-
headers[name] = HeaderUtilities.FormatDate(value.Value);
141+
headers[name] = HeaderUtilities.FormatDate(value.GetValueOrDefault());
142142
}
143143
else
144144
{

src/Microsoft.AspNetCore.Http.Extensions/SendFileResponseExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static void CheckRange(long offset, long? count, long fileLength)
172172
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
173173
}
174174
if (count.HasValue &&
175-
(count.Value < 0 || count.Value > fileLength - offset))
175+
(count.GetValueOrDefault() < 0 || count.GetValueOrDefault() > fileLength - offset))
176176
{
177177
throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
178178
}

src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public static async Task CopyToAsync(Stream source, Stream destination, long? co
4242
{
4343
Debug.Assert(source != null);
4444
Debug.Assert(destination != null);
45-
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0);
45+
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.GetValueOrDefault() >= 0);
4646
Debug.Assert(buffer != null);
4747

4848
while (true)
4949
{
5050
// The natural end of the range.
51-
if (bytesRemaining.HasValue && bytesRemaining.Value <= 0)
51+
if (bytesRemaining.HasValue && bytesRemaining.GetValueOrDefault() <= 0)
5252
{
5353
return;
5454
}
@@ -58,7 +58,7 @@ public static async Task CopyToAsync(Stream source, Stream destination, long? co
5858
int readLength = buffer.Length;
5959
if (bytesRemaining.HasValue)
6060
{
61-
readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength);
61+
readLength = (int)Math.Min(bytesRemaining.GetValueOrDefault(), (long)readLength);
6262
}
6363
int read = await source.ReadAsync(buffer, 0, readLength, cancel);
6464

src/Microsoft.AspNetCore.Http/Features/FormFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancell
199199
if (section.BaseStreamOffset.HasValue)
200200
{
201201
// Relative reference to buffered request body
202-
file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName);
202+
file = new FormFile(_request.Body, section.BaseStreamOffset.GetValueOrDefault(), section.Body.Length, name, fileName);
203203
}
204204
else
205205
{

src/Microsoft.AspNetCore.Http/HeaderDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public long? ContentLength
121121
ThrowIfReadOnly();
122122
if (value.HasValue)
123123
{
124-
this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.Value);
124+
this[HeaderNames.ContentLength] = HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault());
125125
}
126126
else
127127
{

src/Microsoft.AspNetCore.Owin/DictionaryStringValuesWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public long? ContentLength
6868
{
6969
if (value.HasValue)
7070
{
71-
Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatNonNegativeInt64(value.Value);
71+
Inner[HeaderNames.ContentLength] = (StringValues)HeaderUtilities.FormatNonNegativeInt64(value.GetValueOrDefault());
7272
}
7373
else
7474
{

src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ private int UpdatePosition(int read)
151151
if (_observedLength < _position)
152152
{
153153
_observedLength = _position;
154-
if (LengthLimit.HasValue && _observedLength > LengthLimit.Value)
154+
if (LengthLimit.HasValue && _observedLength > LengthLimit.GetValueOrDefault())
155155
{
156-
throw new InvalidDataException($"Multipart body length limit {LengthLimit.Value} exceeded.");
156+
throw new InvalidDataException($"Multipart body length limit {LengthLimit.GetValueOrDefault()} exceeded.");
157157
}
158158
}
159159
return read;

src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public static async Task DrainAsync(this Stream stream, ArrayPool<byte> bytePool
3434
{
3535
// Not all streams support cancellation directly.
3636
cancellationToken.ThrowIfCancellationRequested();
37-
if (limit.HasValue && limit.Value - total < read)
37+
if (limit.HasValue && limit.GetValueOrDefault() - total < read)
3838
{
39-
throw new InvalidDataException($"The stream exceeded the data limit {limit.Value}.");
39+
throw new InvalidDataException($"The stream exceeded the data limit {limit.GetValueOrDefault()}.");
4040
}
4141
total += read;
4242
read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);

src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ public override string ToString()
197197
{
198198
AppendValueWithSeparatorIfRequired(sb, MaxAgeString);
199199
sb.Append('=');
200-
sb.Append(((int)_maxAge.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
200+
sb.Append(((int)_maxAge.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
201201
}
202202

203203
if (_sharedMaxAge.HasValue)
204204
{
205205
AppendValueWithSeparatorIfRequired(sb, SharedMaxAgeString);
206206
sb.Append('=');
207-
sb.Append(((int)_sharedMaxAge.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
207+
sb.Append(((int)_sharedMaxAge.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
208208
}
209209

210210
if (_maxStale)
@@ -213,15 +213,15 @@ public override string ToString()
213213
if (_maxStaleLimit.HasValue)
214214
{
215215
sb.Append('=');
216-
sb.Append(((int)_maxStaleLimit.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
216+
sb.Append(((int)_maxStaleLimit.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
217217
}
218218
}
219219

220220
if (_minFresh.HasValue)
221221
{
222222
AppendValueWithSeparatorIfRequired(sb, MinFreshString);
223223
sb.Append('=');
224-
sb.Append(((int)_minFresh.Value.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
224+
sb.Append(((int)_minFresh.GetValueOrDefault().TotalSeconds).ToString(NumberFormatInfo.InvariantInfo));
225225
}
226226

227227
if (_private)
@@ -291,10 +291,10 @@ public override int GetHashCode()
291291

292292
// XOR the hashcode of timespan values with different numbers to make sure two instances with the same
293293
// timespan set on different fields result in different hashcodes.
294-
result = result ^ (_maxAge.HasValue ? _maxAge.Value.GetHashCode() ^ 1 : 0) ^
295-
(_sharedMaxAge.HasValue ? _sharedMaxAge.Value.GetHashCode() ^ 2 : 0) ^
296-
(_maxStaleLimit.HasValue ? _maxStaleLimit.Value.GetHashCode() ^ 4 : 0) ^
297-
(_minFresh.HasValue ? _minFresh.Value.GetHashCode() ^ 8 : 0);
294+
result = result ^ (_maxAge.HasValue ? _maxAge.GetValueOrDefault().GetHashCode() ^ 1 : 0) ^
295+
(_sharedMaxAge.HasValue ? _sharedMaxAge.GetValueOrDefault().GetHashCode() ^ 2 : 0) ^
296+
(_maxStaleLimit.HasValue ? _maxStaleLimit.GetValueOrDefault().GetHashCode() ^ 4 : 0) ^
297+
(_minFresh.HasValue ? _minFresh.GetValueOrDefault().GetHashCode() ^ 8 : 0);
298298

299299
if ((_noCacheHeaders != null) && (_noCacheHeaders.Count > 0))
300300
{

src/Microsoft.Net.Http.Headers/ContentDispositionHeaderValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ public long? Size
137137
}
138138
else if (sizeParameter != null)
139139
{
140-
sizeParameter.Value = value.Value.ToString(CultureInfo.InvariantCulture);
140+
sizeParameter.Value = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture);
141141
}
142142
else
143143
{
144-
string sizeString = value.Value.ToString(CultureInfo.InvariantCulture);
144+
string sizeString = value.GetValueOrDefault().ToString(CultureInfo.InvariantCulture);
145145
_parameters.Add(new NameValueHeaderValue(SizeString, sizeString));
146146
}
147147
}
@@ -324,7 +324,7 @@ private void SetDate(string parameter, DateTimeOffset? date)
324324
else
325325
{
326326
// Must always be quoted
327-
var dateString = HeaderUtilities.FormatDate(date.Value, quoted: true);
327+
var dateString = HeaderUtilities.FormatDate(date.GetValueOrDefault(), quoted: true);
328328
if (dateParameter != null)
329329
{
330330
dateParameter.Value = dateString;

src/Microsoft.Net.Http.Headers/ContentRangeHeaderValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public override string ToString()
151151

152152
if (HasRange)
153153
{
154-
sb.Append(_from.Value.ToString(NumberFormatInfo.InvariantInfo));
154+
sb.Append(_from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo));
155155
sb.Append('-');
156-
sb.Append(_to.Value.ToString(NumberFormatInfo.InvariantInfo));
156+
sb.Append(_to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo));
157157
}
158158
else
159159
{
@@ -163,7 +163,7 @@ public override string ToString()
163163
sb.Append('/');
164164
if (HasLength)
165165
{
166-
sb.Append(_length.Value.ToString(NumberFormatInfo.InvariantInfo));
166+
sb.Append(_length.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo));
167167
}
168168
else
169169
{

src/Microsoft.Net.Http.Headers/RangeConditionHeaderValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override string ToString()
5454
{
5555
if (_entityTag == null)
5656
{
57-
return HeaderUtilities.FormatDate(_lastModified.Value);
57+
return HeaderUtilities.FormatDate(_lastModified.GetValueOrDefault());
5858
}
5959
return _entityTag.ToString();
6060
}
@@ -70,7 +70,7 @@ public override bool Equals(object obj)
7070

7171
if (_entityTag == null)
7272
{
73-
return (other._lastModified != null) && (_lastModified.Value == other._lastModified.Value);
73+
return (other._lastModified != null) && (_lastModified.GetValueOrDefault() == other._lastModified.GetValueOrDefault());
7474
}
7575

7676
return _entityTag.Equals(other._entityTag);
@@ -80,7 +80,7 @@ public override int GetHashCode()
8080
{
8181
if (_entityTag == null)
8282
{
83-
return _lastModified.Value.GetHashCode();
83+
return _lastModified.GetValueOrDefault().GetHashCode();
8484
}
8585

8686
return _entityTag.GetHashCode();

src/Microsoft.Net.Http.Headers/RangeItemHeaderValue.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public RangeItemHeaderValue(long? from, long? to)
2020
{
2121
throw new ArgumentException("Invalid header range.");
2222
}
23-
if (from.HasValue && (from.Value < 0))
23+
if (from.HasValue && (from.GetValueOrDefault() < 0))
2424
{
2525
throw new ArgumentOutOfRangeException(nameof(from));
2626
}
27-
if (to.HasValue && (to.Value < 0))
27+
if (to.HasValue && (to.GetValueOrDefault() < 0))
2828
{
2929
throw new ArgumentOutOfRangeException(nameof(to));
3030
}
31-
if (from.HasValue && to.HasValue && (from.Value > to.Value))
31+
if (from.HasValue && to.HasValue && (from.GetValueOrDefault() > to.GetValueOrDefault()))
3232
{
3333
throw new ArgumentOutOfRangeException(nameof(from));
3434
}
@@ -51,38 +51,37 @@ public override string ToString()
5151
{
5252
if (!_from.HasValue)
5353
{
54-
return "-" + _to.Value.ToString(NumberFormatInfo.InvariantInfo);
54+
return "-" + _to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo);
5555
}
5656
else if (!_to.HasValue)
5757
{
58-
return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-";
58+
return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-";
5959
}
60-
return _from.Value.ToString(NumberFormatInfo.InvariantInfo) + "-" +
61-
_to.Value.ToString(NumberFormatInfo.InvariantInfo);
60+
return _from.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo) + "-" +
61+
_to.GetValueOrDefault().ToString(NumberFormatInfo.InvariantInfo);
6262
}
6363

6464
public override bool Equals(object obj)
6565
{
66-
var other = obj as RangeItemHeaderValue;
67-
68-
if (other == null)
66+
if (obj is RangeItemHeaderValue other)
6967
{
70-
return false;
68+
return ((_from == other._from) && (_to == other._to));
7169
}
72-
return ((_from == other._from) && (_to == other._to));
70+
71+
return false;
7372
}
7473

7574
public override int GetHashCode()
7675
{
7776
if (!_from.HasValue)
7877
{
79-
return _to.GetHashCode();
78+
return _to.GetValueOrDefault().GetHashCode();
8079
}
8180
else if (!_to.HasValue)
8281
{
83-
return _from.GetHashCode();
82+
return _from.GetValueOrDefault().GetHashCode();
8483
}
85-
return _from.GetHashCode() ^ _to.GetHashCode();
84+
return _from.GetValueOrDefault().GetHashCode() ^ _to.GetValueOrDefault().GetHashCode();
8685
}
8786

8887
// Returns the length of a range list. E.g. "1-2, 3-4, 5-6" adds 3 ranges to 'rangeCollection'. Note that empty

src/Microsoft.Net.Http.Headers/SetCookieHeaderValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ public override string ToString()
105105

106106
if (Expires.HasValue)
107107
{
108-
expires = HeaderUtilities.FormatDate(Expires.Value);
108+
expires = HeaderUtilities.FormatDate(Expires.GetValueOrDefault());
109109
length += SeparatorToken.Length + ExpiresToken.Length + EqualsToken.Length + expires.Length;
110110
}
111111

112112
if (MaxAge.HasValue)
113113
{
114-
maxAge = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds);
114+
maxAge = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds);
115115
length += SeparatorToken.Length + MaxAgeToken.Length + EqualsToken.Length + maxAge.Length;
116116
}
117117

@@ -212,12 +212,12 @@ public void AppendToStringBuilder(StringBuilder builder)
212212

213213
if (Expires.HasValue)
214214
{
215-
AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value));
215+
AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.GetValueOrDefault()));
216216
}
217217

218218
if (MaxAge.HasValue)
219219
{
220-
AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds));
220+
AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds));
221221
}
222222

223223
if (Domain != null)

src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override string ToString()
5858
{
5959
if (_quality.HasValue)
6060
{
61-
return _value + "; q=" + _quality.Value.ToString("0.0##", NumberFormatInfo.InvariantInfo);
61+
return _value + "; q=" + _quality.GetValueOrDefault().ToString("0.0##", NumberFormatInfo.InvariantInfo);
6262
}
6363

6464
return _value.ToString();
@@ -83,7 +83,7 @@ public override bool Equals(object obj)
8383
// Note that we don't consider double.Epsilon here. We really consider two values equal if they're
8484
// actually equal. This makes sure that we also get the same hashcode for two values considered equal
8585
// by Equals().
86-
return other._quality.HasValue && (_quality.Value == other._quality.Value);
86+
return other._quality.HasValue && (_quality.GetValueOrDefault() == other._quality.Value);
8787
}
8888

8989
// If we don't have a quality value, then 'other' must also have no quality assigned in order to be
@@ -97,7 +97,7 @@ public override int GetHashCode()
9797

9898
if (_quality.HasValue)
9999
{
100-
result = result ^ _quality.Value.GetHashCode();
100+
result = result ^ _quality.GetValueOrDefault().GetHashCode();
101101
}
102102

103103
return result;

0 commit comments

Comments
 (0)