开发者

Is VB's Dim the same as C#'s var?

This is a small doubt: On VB, I can declare variables using Dim. In C#, I can declare then using var. I'm more familiar with the intricacies of C#, and I know that var uses type inference to determine the type of the variable. But I'm not sure what 'Dim' does.

I've seem this question on SO, but it doesn't compare both keywords. If there's a difference, can s开发者_如何学Pythonomeone tell me which?


That depends if Option Infer is specified. Normally, the following are equivalent:

'VB.Net
Dim myVar
Dim myString = "Hello world!"
Dim myString2 As String = "Hello world!"
//C#
object myVar;
object myString = "Hello world!"; //Notice type is object, *not* string!
string myString2 = "Hello world!";

However, with Option Infer enabled, Dim becomes more like var when the variable is initialized on the same line:

'VB.Net
Option Infer On
Dim myVar
Dim myString = "Hello!"
//C#
object myVar;
var myString = "Hello!"; //Or the equivalent:  string myString = "Hello!";

Note that this can lead to some confusion because suddenly initializing a variable at the point of declaration means something different from initializing it later:

'VB.Net
Option Infer On
Dim myVar1
myVar1 = 10
Dim myVar2 = 10

myVar1 = New MyClass() 'Legal
myVar2 = New MyClass() 'Illegal! - Value of type 'MyClass' cannot be converted to 'Integer'

This can be fixed by enabling Option Strict, which (among other things) forces all variables to be given a type (implicitly or not) at the time of declaration.


They are not the same. Dim in VB simply means that what follows is a variable declaration.

For example, these two are equivalent:

Dim x As String = "foo"
string x = "foo"

In C#, var means that a variable declaration’s type is inferred by the compiler based on usage (initialisation). The same can be achieved in VB by simply omitting the type of the declaration. However, this also requires that Option Strict and Option Infer are activated:

Dim x = "bar" ' Compiler infers type of x = string
var x = "bar" // same here.


It depends, in vb.net you write

dim x = 1 

then it's the same as c# var.

Or you can write

dim x as integer = 1 

which is the same as c#

int x = 1;

As from vb.net 9.0 you don't need to have the type with an initialize statement

http://msdn.microsoft.com/en-us/library/ms364068(v=vs.80).aspx


There's this from Scott Hanselman

For c# var:

...[var is] a new keyword that means, "I want to declare a variable, but I’m too lazy to write out its type."

One way to look at the power of VB's Dim operator is to say,

Dim kind of means, "I want to declare a variable but I can't tell you much about how it behaves until much later."

Dim lets you do actual late-binding while in C# (today) you do late-binding with reflection.


The Dim in VB doesn't do anything itself, its just a keyword that means the rest of the statement is to do with declaring a variable. Its a BASIC keyword from the mists of time meaning 'dimension' when you couldn't declare ordinary variables, but you did have to declare the 'dimension' of arrays.

If, in your mind's eye, you ignore the Dim and compare what's left you'll find VB and C# pretty similar, except that C# does it the wrong way round (You can guess which language I grew up with) and, like C, puts the type first (or uses var) and the name of the variable next.


Dim means 'dimension', and is used in VB.Net to declare a variable. It basically implies 'Reservce some/enough space to hold one a type of __'. The datatype comes in later; usually on the same line.

var in C#, as you mentioned, is actually a shortcut to tell the compiler to infer the data type based on the value first assigned.


the var keyword is specifically typed by the datatype it is assigned in the variable. But dim is invariant which can be used for any type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜