VB.Net does not throw compiler error when setting Dictionary to IList
I am updating a legacy application, and it was reading a dll from another project for a Dictionary(of Guid, Str开发者_JS百科ing) of items and using them.
The requirements have changed, and the method that was returning the Dictionary is now returning an IList.
This is the odd behavior of this; the intellisense is not throwing a cast error, nor is the compiler. It does not throw an error until runtime when it tries to set the Dictionary to the IList.
Example:
Dim someDictionary As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
Dim someList As IList(Of Integer)
someDictionary = someList
Any idea as to why the compiler is not catching this?
It gives an error when "Option Strict" is On:
Option Strict On
Imports System.Collections.Generic
Public Class Test
Public Shared Sub Main()
Dim someDictionary As Dictionary(Of Integer, String) = _
New Dictionary(Of Integer, String)
Dim someList As IList(Of Integer) = Nothing
someDictionary = someList
End Sub
End Class
Error:
error BC30512: Option Strict On disallows implicit conversions from 'System.Collections.Generic.IList(Of Integer)' to 'System.Collections.Generic.Dictionary(Of Integer, String)'.
someDictionary = someList
I suggest you change your project to enable Option Strict, to help catch this sort of thing :)
精彩评论