Home Ask Login Register

Developers Planet

Your answer is one click away!

Maiken Roskilde February 2016

Why does System.IO.Directory.GetFiles does not have a List(Of String) return overload?

I have only found overloads that return an array of strings. Is there a specific reason why there is no List(Of String)?

I have to write

Dim nNewStringArray() As String
ReDim nNewStringArray(0)
nNewStringArray = System.IO.Directory.GetFiles(uInitDir)

instead of being able to just write

Dim nNewListOfString As New List(Of String) = System.IO.Directory.GetFiles(uInitDir)

I feel that Redim is a bit old school.


D Stanley February 2016

I can think of 3 reasons (in no particular order):

  1. When the method was developed there were no generics, and thus no List<T>. So string[] was the best strongly-typed option available at the time.

  2. You can't overload methods by return type, so it would have to either be called something like GetListOfFiles() (bad) or be a breaking change (very bad)

  3. it's just as easy to do:

    Dim nNewListOfString As New List(Of String) (System.IO.Directory.GetFiles(uInitDir))

Plus you don't need to ReDim your array as mentioned in the comments to your question. You can just do:

Dim nNewStringArray() As String
nNewStringArray = System.IO.Directory.GetFiles(uInitDir)

or just

Dim nNewStringArray() As String = System.IO.Directory.GetFiles(uInitDir)

if you prefer. The only reason I would turn the results into a List is if I were going to add items to it. Pretty much anything else can be done with an array.

Post Status

Asked in February 2016
Viewed 3,051 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life