nubela nubela - 3 months ago 39
C# Question

What is the equivalent of VB's "Dim" statement in C#?

Picking up C#, can't seem to find any useful reference to this, other than examples.

So, what is Dim in C#?

Answer Source

In VB, Dim declares a variable of a particular type (or of variable type, if you don't specify one). If you Dim x as Foo, that declares a variable of type Foo called x.

In C#, the equivalent is to state the type followed by the variable's name, as in:

Foo x;
int i;

You can also assign in the same step:

Foo x = new Foo();
int i = 6;

C# supports type inference, so you can also do:

// Compiler infers type of x and i based on this assignment.
var x = new Foo(); // x is now of type Foo
var i = 10;        // i is now of type int

A Dim without a corresponding type in VB is similar to declaring a type as Object in C#:

object o = ...; // An object instance can hold any value or reference type.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download