Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

So, what is Dim in C#?

like image 920
nubela Avatar asked Jan 24 '10 16:01

nubela


People also ask

What is dim in C language?

The Visual Basic compiler uses the Dim statement to determine the variable's data type and other information, such as what code can access the variable. The following example declares a variable to hold an Integer value. You can specify any data type or the name of an enumeration, structure, class, or interface.

What is a dim statement?

Description. Declares and allocates storage space in memory for variables. The Dim statement is used either at the start of a procedure or the start of a module to declare a variable of a particular data type.

In which condition is dim statement used?

Use the Dim statement at the module or procedure level to declare the data type of a variable. For example, the following statement declares a variable as an Integer. Also use a Dim statement to declare the object type of a variable. The following declares a variable for a new instance of a worksheet.

What is dim in dot net?

Variable Declaration in VB.Net. The Dim statement is used for variable declaration and storage allocation for one or more variables. The Dim statement is used at module, class, structure, procedure or block level.


1 Answers

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.
like image 171
John Feminella Avatar answered Oct 14 '22 08:10

John Feminella