3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System . ComponentModel ;
6
+ using System . Diagnostics ;
6
7
7
8
namespace System . Windows
8
9
{
@@ -11,10 +12,10 @@ namespace System.Windows
11
12
/// This structure may represent a TimeSpan, Automatic, or Forever value.
12
13
/// </summary>
13
14
[ TypeConverter ( typeof ( DurationConverter ) ) ]
14
- public struct Duration
15
+ public readonly struct Duration
15
16
{
16
- private TimeSpan _timeSpan ;
17
- private DurationType _durationType ;
17
+ private readonly TimeSpan _timeSpan ;
18
+ private readonly DurationType _durationType ;
18
19
19
20
/// <summary>
20
21
/// Creates a Duration from a TimeSpan.
@@ -23,13 +24,23 @@ public struct Duration
23
24
public Duration ( TimeSpan timeSpan )
24
25
{
25
26
if ( timeSpan < TimeSpan . Zero )
26
- {
27
- throw new ArgumentException ( SR . Timing_InvalidArgNonNegative , "timeSpan" ) ;
28
- }
27
+ throw new ArgumentException ( SR . Timing_InvalidArgNonNegative , nameof ( timeSpan ) ) ;
28
+
29
29
_durationType = DurationType . TimeSpan ;
30
30
_timeSpan = timeSpan ;
31
31
}
32
32
33
+ /// <summary>
34
+ /// Private constructor, server for creation of <see cref="Duration.Automatic"/> and <see cref="Duration.Forever"/> only.
35
+ /// </summary>
36
+ /// <param name="durationType">Only <see cref="Duration.Automatic"/> and <see cref="Duration.Forever"/> values are permitted.</param>
37
+ private Duration ( DurationType durationType )
38
+ {
39
+ Debug . Assert ( durationType == DurationType . Automatic || durationType == DurationType . Forever ) ;
40
+
41
+ _durationType = durationType ;
42
+ }
43
+
33
44
#region Operators
34
45
35
46
//
@@ -39,7 +50,7 @@ public Duration(TimeSpan timeSpan)
39
50
// Any comparision with Automatic returns false, except for ==.
40
51
// Unlike NaN, Automatic == Automatic is true.
41
52
//
42
-
53
+
43
54
44
55
/// <summary>
45
56
/// Implicitly creates a Duration from a TimeSpan.
@@ -49,9 +60,8 @@ public Duration(TimeSpan timeSpan)
49
60
public static implicit operator Duration ( TimeSpan timeSpan )
50
61
{
51
62
if ( timeSpan < TimeSpan . Zero )
52
- {
53
- throw new ArgumentException ( SR . Timing_InvalidArgNonNegative , "timeSpan" ) ;
54
- }
63
+ throw new ArgumentException ( SR . Timing_InvalidArgNonNegative , nameof ( timeSpan ) ) ;
64
+
55
65
return new Duration ( timeSpan ) ;
56
66
}
57
67
@@ -346,7 +356,7 @@ public bool HasTimeSpan
346
356
{
347
357
get
348
358
{
349
- return ( _durationType == DurationType . TimeSpan ) ;
359
+ return _durationType == DurationType . TimeSpan ;
350
360
}
351
361
}
352
362
@@ -358,12 +368,7 @@ public static Duration Automatic
358
368
{
359
369
get
360
370
{
361
- Duration duration = new Duration
362
- {
363
- _durationType = DurationType . Automatic
364
- } ;
365
-
366
- return duration ;
371
+ return new Duration ( DurationType . Automatic ) ;
367
372
}
368
373
}
369
374
@@ -375,32 +380,20 @@ public static Duration Forever
375
380
{
376
381
get
377
382
{
378
- Duration duration = new Duration
379
- {
380
- _durationType = DurationType . Forever
381
- } ;
382
-
383
- return duration ;
383
+ return new Duration ( DurationType . Forever ) ;
384
384
}
385
385
}
386
386
387
387
/// <summary>
388
388
/// Returns the TimeSpan value that this Duration represents.
389
389
/// </summary>
390
390
/// <value>The TimeSpan value that this Duration represents.</value>
391
- /// <exception cref="System. InvalidOperationException">Thrown if this Duration represents null.</exception>
391
+ /// <exception cref="InvalidOperationException">Thrown if this Duration represents null.</exception>
392
392
public TimeSpan TimeSpan
393
393
{
394
394
get
395
395
{
396
- if ( HasTimeSpan )
397
- {
398
- return _timeSpan ;
399
- }
400
- else
401
- {
402
- throw new InvalidOperationException ( SR . Format ( SR . Timing_NotTimeSpan , this ) ) ;
403
- }
396
+ return HasTimeSpan ? _timeSpan : throw new InvalidOperationException ( SR . Format ( SR . Timing_NotTimeSpan , this ) ) ;
404
397
}
405
398
}
406
399
@@ -423,20 +416,9 @@ public Duration Add(Duration duration)
423
416
/// </summary>
424
417
/// <param name="value"></param>
425
418
/// <returns>true if value is a Duration and is equal to this instance; otherwise false.</returns>
426
- public override bool Equals ( Object value )
419
+ public override bool Equals ( object value )
427
420
{
428
- if ( value == null )
429
- {
430
- return false ;
431
- }
432
- else if ( value is Duration )
433
- {
434
- return Equals ( ( Duration ) value ) ;
435
- }
436
- else
437
- {
438
- return false ;
439
- }
421
+ return value is Duration duration && Equals ( duration ) ;
440
422
}
441
423
442
424
/// <summary>
@@ -480,14 +462,7 @@ public static bool Equals(Duration t1, Duration t2)
480
462
/// <returns>A 32-bit signed integer hash code.</returns>
481
463
public override int GetHashCode ( )
482
464
{
483
- if ( HasTimeSpan )
484
- {
485
- return _timeSpan . GetHashCode ( ) ;
486
- }
487
- else
488
- {
489
- return _durationType . GetHashCode ( ) + 17 ;
490
- }
465
+ return HasTimeSpan ? _timeSpan . GetHashCode ( ) : _durationType . GetHashCode ( ) + 17 ;
491
466
}
492
467
493
468
/// <summary>
0 commit comments