@@ -519,6 +519,10 @@ public override int Read(byte[] buffer, int offset, int count)
519519 throw new ArgumentNullException ( nameof ( buffer ) ) ;
520520 }
521521
522+ #if NET8_0_OR_GREATER
523+ ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
524+ ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
525+ #else
522526 if ( offset < 0 )
523527 {
524528 throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
@@ -528,7 +532,7 @@ public override int Read(byte[] buffer, int offset, int count)
528532 {
529533 throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
530534 }
531-
535+ #endif
532536 if ( ( buffer . Length - offset ) < count )
533537 {
534538 throw new ArgumentException ( "Invalid array range." ) ;
@@ -660,6 +664,10 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
660664 throw new ArgumentNullException ( nameof ( buffer ) ) ;
661665 }
662666
667+ #if NET8_0_OR_GREATER
668+ ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
669+ ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
670+ #else
663671 if ( offset < 0 )
664672 {
665673 throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
@@ -669,7 +677,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
669677 {
670678 throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
671679 }
672-
680+ #endif
673681 if ( ( buffer . Length - offset ) < count )
674682 {
675683 throw new ArgumentException ( "Invalid array range." ) ;
@@ -951,10 +959,14 @@ public override long Seek(long offset, SeekOrigin origin)
951959 /// </remarks>
952960 public override void SetLength ( long value )
953961 {
962+ #if NET8_0_OR_GREATER
963+ ArgumentOutOfRangeException . ThrowIfNegative ( value ) ;
964+ #else
954965 if ( value < 0 )
955966 {
956967 throw new ArgumentOutOfRangeException ( nameof ( value ) ) ;
957968 }
969+ #endif
958970
959971 // Lock down the file stream while we do this.
960972 lock ( _lock )
@@ -1005,6 +1017,10 @@ public override void Write(byte[] buffer, int offset, int count)
10051017 throw new ArgumentNullException ( nameof ( buffer ) ) ;
10061018 }
10071019
1020+ #if NET8_0_OR_GREATER
1021+ ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
1022+ ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
1023+ #else
10081024 if ( offset < 0 )
10091025 {
10101026 throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
@@ -1014,7 +1030,7 @@ public override void Write(byte[] buffer, int offset, int count)
10141030 {
10151031 throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
10161032 }
1017-
1033+ #endif
10181034 if ( ( buffer . Length - offset ) < count )
10191035 {
10201036 throw new ArgumentException ( "Invalid array range." ) ;
@@ -1104,6 +1120,10 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
11041120 throw new ArgumentNullException ( nameof ( buffer ) ) ;
11051121 }
11061122
1123+ #if NET8_0_OR_GREATER
1124+ ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
1125+ ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
1126+ #else
11071127 if ( offset < 0 )
11081128 {
11091129 throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
@@ -1113,7 +1133,7 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
11131133 {
11141134 throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
11151135 }
1116-
1136+ #endif
11171137 if ( ( buffer . Length - offset ) < count )
11181138 {
11191139 throw new ArgumentException ( "Invalid array range." ) ;
0 commit comments