Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1b2810b

Browse files
authored
Replace several Nullable<T>.Value with .GetValueOrDefault() (#22297)
The former does extra work that the latter doesn't do, and they're equivalent when we know it contains a value, such as immediately after a HasValue check.
1 parent 03bc819 commit 1b2810b

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

src/System.Private.CoreLib/shared/System/IO/FileStream.Unix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private bool CanSeekCore(SafeFileHandle fileHandle)
209209
_canSeek = Interop.Sys.LSeek(fileHandle, 0, Interop.Sys.SeekWhence.SEEK_CUR) >= 0;
210210
}
211211

212-
return _canSeek.Value;
212+
return _canSeek.GetValueOrDefault();
213213
}
214214

215215
private long GetLengthInternal()

src/System.Private.CoreLib/shared/System/IO/FileStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private void ValidateAndInitFromHandle(SafeFileHandle handle, FileAccess access,
135135

136136
if (handle.IsClosed)
137137
throw new ObjectDisposedException(SR.ObjectDisposed_FileClosed);
138-
if (handle.IsAsync.HasValue && isAsync != handle.IsAsync.Value)
138+
if (handle.IsAsync.HasValue && isAsync != handle.IsAsync.GetValueOrDefault())
139139
throw new ArgumentException(SR.Arg_HandleNotAsync, nameof(handle));
140140

141141
_exposedHandle = true;

src/System.Private.CoreLib/shared/System/TimeZoneInfo.Unix.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ private static AdjustmentRule TZif_CreateAdjustmentRuleForPosixFormat(string pos
10951095
TimeSpan? parsedBaseOffset = TZif_ParseOffsetString(standardOffset);
10961096
if (parsedBaseOffset.HasValue)
10971097
{
1098-
TimeSpan baseOffset = parsedBaseOffset.Value.Negate(); // offsets are backwards in POSIX notation
1098+
TimeSpan baseOffset = parsedBaseOffset.GetValueOrDefault().Negate(); // offsets are backwards in POSIX notation
10991099
baseOffset = TZif_CalculateTransitionOffsetFromBase(baseOffset, timeZoneBaseUtcOffset);
11001100

11011101
// having a daylightSavingsName means there is a DST rule
@@ -1110,7 +1110,7 @@ private static AdjustmentRule TZif_CreateAdjustmentRuleForPosixFormat(string pos
11101110
}
11111111
else
11121112
{
1113-
daylightSavingsTimeSpan = parsedDaylightSavings.Value.Negate(); // offsets are backwards in POSIX notation
1113+
daylightSavingsTimeSpan = parsedDaylightSavings.GetValueOrDefault().Negate(); // offsets are backwards in POSIX notation
11141114
daylightSavingsTimeSpan = TZif_CalculateTransitionOffsetFromBase(daylightSavingsTimeSpan, timeZoneBaseUtcOffset);
11151115
daylightSavingsTimeSpan = TZif_CalculateTransitionOffsetFromBase(daylightSavingsTimeSpan, baseOffset);
11161116
}
@@ -1176,7 +1176,7 @@ private static AdjustmentRule TZif_CreateAdjustmentRuleForPosixFormat(string pos
11761176

11771177
if (result.HasValue && negative)
11781178
{
1179-
result = result.Value.Negate();
1179+
result = result.GetValueOrDefault().Negate();
11801180
}
11811181
}
11821182

@@ -1193,8 +1193,8 @@ private static DateTime ParseTimeOfDay(ReadOnlySpan<char> time)
11931193
// Some time zones use time values like, "26", "144", or "-2".
11941194
// This allows the week to sometimes be week 4 and sometimes week 5 in the month.
11951195
// For now, strip off any 'days' in the offset, and just get the time of day correct
1196-
timeOffset = new TimeSpan(timeOffset.Value.Hours, timeOffset.Value.Minutes, timeOffset.Value.Seconds);
1197-
if (timeOffset.Value < TimeSpan.Zero)
1196+
timeOffset = new TimeSpan(timeOffset.GetValueOrDefault().Hours, timeOffset.GetValueOrDefault().Minutes, timeOffset.GetValueOrDefault().Seconds);
1197+
if (timeOffset.GetValueOrDefault() < TimeSpan.Zero)
11981198
{
11991199
timeOfDay = new DateTime(1, 1, 2, 0, 0, 0);
12001200
}
@@ -1203,7 +1203,7 @@ private static DateTime ParseTimeOfDay(ReadOnlySpan<char> time)
12031203
timeOfDay = new DateTime(1, 1, 1, 0, 0, 0);
12041204
}
12051205

1206-
timeOfDay += timeOffset.Value;
1206+
timeOfDay += timeOffset.GetValueOrDefault();
12071207
}
12081208
else
12091209
{

src/System.Private.CoreLib/shared/System/TimeZoneInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ private AdjustmentRule GetPreviousAdjustmentRule(AdjustmentRule rule, int? ruleI
286286
{
287287
Debug.Assert(rule.NoDaylightTransitions, "GetPreviousAdjustmentRule should only be used with NoDaylightTransitions rules.");
288288

289-
if (ruleIndex.HasValue && 0 < ruleIndex.Value && ruleIndex.Value < _adjustmentRules.Length)
289+
if (ruleIndex.HasValue && 0 < ruleIndex.GetValueOrDefault() && ruleIndex.GetValueOrDefault() < _adjustmentRules.Length)
290290
{
291-
return _adjustmentRules[ruleIndex.Value - 1];
291+
return _adjustmentRules[ruleIndex.GetValueOrDefault() - 1];
292292
}
293293

294294
AdjustmentRule result = rule;

src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/CLRIReferenceImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ internal static object CreateIReference(object obj)
296296
if (propType.HasValue)
297297
{
298298
Type specificType = typeof(CLRIReferenceImpl<>).MakeGenericType(type);
299-
return Activator.CreateInstance(specificType, new object[] { propType.Value, obj });
299+
return Activator.CreateInstance(specificType, new object[] { propType.GetValueOrDefault(), obj });
300300
}
301301

302302
Debug.Fail("We should not see non-WinRT type here");
@@ -389,7 +389,7 @@ internal static object CreateIReferenceArray(Array obj)
389389
{
390390
// All WinRT value type will be Property.Other
391391
Type specificType = typeof(CLRIReferenceArrayImpl<>).MakeGenericType(type);
392-
return Activator.CreateInstance(specificType, new object[] { propType.Value, obj });
392+
return Activator.CreateInstance(specificType, new object[] { propType.GetValueOrDefault(), obj });
393393
}
394394
else
395395
{

0 commit comments

Comments
 (0)