1

This question is regarding implicit conversions in VB.Net and C#. The following C# code does not compile:

class Program
{
    static void Foo(string s)
    {
        Console.WriteLine(s);
    }

    static void Main(string[] args)
    {
        Foo(3); // Cannot convert from int to string
    }
}

The reason being that Foo expects a parameter of type string, but its given an int. A solution is to replace Foo(3) with Foo(3.ToString()). Fine.

Now, the same code authored in VB.Net:

Module Module1
    Sub Foo(s As String)
        Console.WriteLine(s)
    End Sub

    Sub Main()
        Foo(5)
    End Sub
End Module

This compiles and runs just fine!

Question: Why does VB.Net allow this and is there some fundamental difference between VB.Net and C# here?

jensa
  • 2,792
  • 2
  • 21
  • 36

1 Answers1

4

Programming languages make different trade-offs. VB.NET with Strict=Off is very lax. This is for historic compatibility reasons.

A few decades ago programming language designers thought that this behavior was helpful to beginners. Now we know that such lax behavior is terrible for program correctness and development speed.

JavaScript suffers from the same kind of problems, but worse.

Use strict semantics.

usr
  • 168,620
  • 35
  • 240
  • 369