0

Error 45 'System.Collections.Generic.List(Of String)' cannot be converted to 'System.Collections.Generic.List(Of Object)'. Consider using 'System.Collections.Generic.IEnumerable(Of Object)' instead. H:\business\shared\Dropbox\vb.net\proxyhandler.vb 198 19 nicehash2

This is the error that I got. Replacing

System.Collections.Generic.List(Of Object)

with

System.Collections.Generic.IEnumerable(Of Object)

solves the problem.

I am aware that the second type is an interface. I am still confused.

But why?

user4951
  • 32,206
  • 53
  • 172
  • 282

1 Answers1

1

Since lists are mutable(you can f.e. add items) you can't assign a List(Of String) to a variable of type List(Of Object)(even if every string is an object).

Every List(Of T) is also an IEnumerable(Of T), but as opposed to the list the interface doesn't support modifications(without casting it back to the real type). That's why the language allows to use the interface but not the list.

Consider what could happen if it was allowed on the list itself(C# link):

Dim giraffes As New List(Of Giraffe)
giraffes.Add(new Giraffe())
Dim animals As List(Of Object) = giraffes ' doesn't compile but what happened otherwise:
animals.Add(new Lion()) ' Aargh!
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939