madcolor madcolor -4 years ago 203 Question

Dim X as New Y vs. Dim X as Y = New Y()


Whats the difference between declaring as new and as something new something i

Sad that I don't know this, but can anyone explain the difference between:

Dim X as New Y


Dim X as Y = New Y()

Answer Source

The first just infers that the type is Y. The second specifies it. This allows you to write things like:

Dim X as IList(Of String) = New List(Of String)

That then limits the scope of the choice to use List(Of String) - which means that later on you might choose to use a different implementation of IList(Of String), and it'll still compile.

Of course, the shorter version does have the benefit of brevity - which can be important if you have very long type names (e.g. using nested generics). It's also required for anonymous types, where you can't express the type of X.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download