Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET equivalent to C# var keyword [duplicate]

People also ask

Is C# and VB.NET the same?

Though C# and VB.NET are syntactically very different, that is where the differences mostly end. Microsoft developed both of these languages to be part of the same . NET Framework development platform. They are both developed, managed, and supported by the same language development team at Microsoft.

Is VB.NET still used 2021?

The answer is yes, but it is mostly used for enterprise applications and by a select few large software companies who still enjoy the benefits of Visual Basic. Microsoft uses Visual Basic as the primary programming language for the Windows operating system.

Will VB.NET be phased out?

Visual Basic (VB.NET) will continue to be supported by Microsoft. (It's not dead.) The language will no longer have new features added to it.

Which is not the operator in Visual Basic?

For numeric expressions, the Not operator inverts the bit values of any numeric expression and sets the corresponding bit in result according to the following table.


Option Infer must be on in order for this to function properly. If so, then omitting the type in VB.NET (Visual Basic 9) will implicitly type the variable.

This is not the same as "Option Strict Off" in previous versions of VB.NET, as the variable is strongly-typed; it's just done so implicitly (like the C# var) keyword.

Dim foo = "foo"

foo is declared as a String.


You need Option Infer On and then just use the Dim keyword, thus:

Dim query = From x In y Where x.z = w Select x

Contrary to some of the other answers, you do not need Option Strict On.

If you're using the VS IDE you can just hover over the variable names, but to get the compile-time types of variables (GetType(variableName) does not compile - "Type '<variablename>' is not defined." - and VarType(variable) is actually just the VB version of variable.GetType() which returns the type of the instance stored in the variable at runtime) I used:

Function MyVarType(Of T)(ByRef Var As T) As Type
    Return GetType(T)
End Function

In detail:

  • without Dim:

    Explicit Off, gives Object

    Explicit On, error "Name '' is not declared."

  • with Dim:

    • Infer On, gives expected types
    • Infer Off:

      Strict On, error "Option Strict On requires all declarations to have an 'As' clasue."

      Strict Off, gives Object

As I mentioned in the comments, there are other reasons why Option Strict On allows Linq to perform more usefully. Specifically, you can't get Into Max(Anon.SomeString) to work with Option Strict Off, though there are a number of workarounds.


Simply use the conventional Dim keyword without a type.

Minimal working example:

Option Strict On ' Always a good idea
Option Infer On ' Required for type inference

Imports System

Module MainModule
    Sub Main()
        Dim i = 42
        Dim s = "Hello"
        Console.WriteLine("{0}, {1}", i.GetType(), s.GetType())
        ' Prints System.Int32, System.String '
    End Sub
End Module