WriteAllLines/ReadAllLines

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

The new .NET 2.0 methods in class System.IO.File WriteAllLines and
ReadAllLines are both taking a string[] as argument or return value.

Wouldn't it be more flexible to use IEnumerable<string> for that? Aren't
there guidelines which are saying so? Am I missing something?

Am I the only one with the feeling that lot of the additions to the 2.0
framework are not well thought through, like the funny DateTime2
structure or System.String now having a Contains(string) method but no
method for containing a char?
 
Well I suppose the great thing about DotNet is you can quickly create your
own toolkit that wraps up both classes and provides that functionality. But
straight away I think you run into the question of would you want
ReadAllLines to return an instance of "IEnumerable<string>" or an instance
of a class that implements that interface - sometimes you may want a
List<string>, sometimes you may want a ReadOnlyCollection<string>.

- Paul
 
Paul said:
Well I suppose the great thing about DotNet is you can quickly create your
own toolkit that wraps up both classes and provides that functionality. But
straight away I think you run into the question of would you want
ReadAllLines to return an instance of "IEnumerable<string>" or an instance
of a class that implements that interface - sometimes you may want a
List<string>, sometimes you may want a ReadOnlyCollection<string>.

- Paul

cody said:
The new .NET 2.0 methods in class System.IO.File WriteAllLines and
ReadAllLines are both taking a string[] as argument or return value.

Wouldn't it be more flexible to use IEnumerable<string> for that? Aren't
there guidelines which are saying so? Am I missing something?

Am I the only one with the feeling that lot of the additions to the 2.0
framework are not well thought through, like the funny DateTime2 structure
or System.String now having a Contains(string) method but no method for
containing a char?

Looking with reflektor into the method I see they create an ArrayList
and then call ToArray() on at before returning it, which wouldn't be
necessary with IEnumerable<string>.

With IEnumerable they also could return a special enumerator where
calling enumerator.MoveNext() would read the next line, which would have
the downside that they would have to keep the file open but this was
just an example of which would be possible.

Also bad is that if you already have an Enumerable or ArrayList or
whatever and you want to call WriteAllLines. You will have to copy your
collection into an array first. You also can't use WriteAllLines with
your own enumerator which yields string values on demand.

My whole point is that these functions are pointless (no pun intended)
They are more than limiting as they are defined, why inventing them in
the first place.

Maybe MS should more often make use of FXCop which would have told them
that they shouldn't make an argument of type array when you only do a
foreach on it.
 
Back
Top