@@ -13,24 +13,24 @@ public CsvReader(StreamReader source)
1313 _source = source ?? throw new ArgumentNullException ( nameof ( source ) ) ;
1414 }
1515
16- StreamReader _source ;
17-
16+ StreamReader ? _source ;
17+ StreamReader Source => _source ?? throw new ObjectDisposedException ( GetType ( ) . ToString ( ) ) ;
1818
1919 public void Dispose ( )
2020 {
2121 _source = null ; // The intention here is if this object is disposed, then prevent further reading.
2222 }
2323
2424 public IEnumerable < string > ReadNextRow ( )
25- => GetNextRow ( _source ) ;
25+ => GetNextRow ( Source ) ;
2626
27- public ValueTask < IEnumerable < string > > ReadNextRowAsync ( )
28- => GetNextRowAsync ( _source ) ;
27+ public ValueTask < IEnumerable < string > ? > ReadNextRowAsync ( )
28+ => GetNextRowAsync ( Source ) ;
2929
3030 public IEnumerable < IEnumerable < string > > ReadRows ( )
3131 {
3232 var s = _source ;
33- if ( s == null )
33+ if ( s is null )
3434 throw new ObjectDisposedException ( GetType ( ) . ToString ( ) ) ;
3535 Contract . EndContractBlock ( ) ;
3636
@@ -40,7 +40,7 @@ public IEnumerable<IEnumerable<string>> ReadRows()
4040
4141 public static bool TryGetNextRow ( StreamReader source , out IEnumerable < string > row )
4242 {
43- if ( source == null )
43+ if ( source is null )
4444 throw new ArgumentNullException ( nameof ( source ) ) ;
4545 Contract . EndContractBlock ( ) ;
4646
@@ -50,24 +50,24 @@ public static bool TryGetNextRow(StreamReader source, out IEnumerable<string> ro
5050 return true ;
5151 }
5252
53- row = null ;
53+ row = null ! ;
5454 return false ;
5555 }
5656
57- public static ValueTask < IEnumerable < string > > GetNextRowAsync ( StreamReader source )
57+ public static ValueTask < IEnumerable < string > ? > GetNextRowAsync ( StreamReader source )
5858 {
59- if ( source == null )
59+ if ( source is null )
6060 throw new ArgumentNullException ( nameof ( source ) ) ;
6161 Contract . EndContractBlock ( ) ;
6262
6363 if ( source . EndOfStream )
64- return new ValueTask < IEnumerable < string > > ( default ( IEnumerable < string > ) ) ;
64+ return new ValueTask < IEnumerable < string > ? > ( default ( IEnumerable < string > ) ) ;
6565
6666 return GetNextRowAsyncCore ( source ) ;
6767 }
6868
69- static async ValueTask < IEnumerable < string > > GetNextRowAsyncCore ( StreamReader source )
70- => source . EndOfStream ? null : CsvUtility . GetLine ( await source . ReadLineAsync ( ) ) ;
69+ static async ValueTask < IEnumerable < string > ? > GetNextRowAsyncCore ( StreamReader source )
70+ => source . EndOfStream ? null : CsvUtility . GetLine ( await source . ReadLineAsync ( ) . ConfigureAwait ( false ) ) ;
7171
7272 public static IEnumerable < string > GetNextRow ( StreamReader source )
7373 {
@@ -77,7 +77,7 @@ public static IEnumerable<string> GetNextRow(StreamReader source)
7777
7878 public static IEnumerable < IEnumerable < string > > GetRows ( StreamReader source )
7979 {
80- if ( source == null )
80+ if ( source is null )
8181 throw new ArgumentNullException ( nameof ( source ) ) ;
8282 Contract . EndContractBlock ( ) ;
8383
@@ -87,18 +87,16 @@ public static IEnumerable<IEnumerable<string>> GetRows(StreamReader source)
8787
8888 public static IEnumerable < IEnumerable < string > > GetRowsFromFile ( string filepath )
8989 {
90- if ( filepath == null )
90+ if ( filepath is null )
9191 throw new ArgumentNullException ( nameof ( filepath ) ) ;
9292 if ( string . IsNullOrWhiteSpace ( filepath ) )
9393 throw new ArgumentException ( "Cannot be empty or only whitespace." , nameof ( filepath ) ) ;
9494 Contract . EndContractBlock ( ) ;
9595
96- if ( File . Exists ( filepath ) )
97- {
98- using ( var s = new StreamReader ( new FileInfo ( filepath ) . OpenRead ( ) ) )
99- foreach ( var line in GetRows ( s ) )
100- yield return line ;
101- }
96+ if ( ! File . Exists ( filepath ) ) yield break ;
97+ using var s = new StreamReader ( new FileInfo ( filepath ) . OpenRead ( ) ) ;
98+ foreach ( var line in GetRows ( s ) )
99+ yield return line ;
102100 }
103101 }
104102}
0 commit comments