Generics in VB.NET

Now, as a C # programmer, I know that generics are awesome. However, when working on some VB.NET, I found that the following does not cause a compiler error:

Dim instance As List(Of Integer) instance.Add(True) 

Why is this? I know that you are not required to throw in VB.NET, but I would think that this kills the main reason for using generic security.

Edit: I do not have a strict mode option, since this was not a real programming exercise, I just theoretically look at VB.NET. This is a theoretical question, since I expected it to cause a compiler error even when the option is disabled, as a feature of generic types.

+6
compiler-construction generics type-safety
source share
3 answers

Without Option Strict On , VB.NET will happily convert Boolean to Integer . I highly recommend (especially against C #) that you do Option Strict On by default for VB.NET to work.

You can do this in Visual Studio in Tools | Options | Projects and Solutions | VB Defaults .

edit to learn more about VB's "laid-back" attitude toward type conversion, google "Evil Type Force". Those of us who wanted to do a good job in VB (classic) had to fight this demon for now ...

+10
source share

Do you have Option Strict enabled?

+3
source share

VB.NET converts function calls to a site. The list (Integer) still stores an integer (-1, which is an integer value True)

0
source share

All Articles