开发者

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

Picking up C#, can't seem to find any useful reference to t开发者_如何学编程his, other than examples.

So, what is Dim in C#?


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.


Assuming that you mean Dim in VB, C# uses either var or the name of the type to declare a new type

string myString = "Hello";
// is the same as
var myString = "Hello"; // type is inferred

Note that the var keyword is not a variant type, like in VB. It is an implicitly typed variable.


This is actually a bit tricky, because Dim can be used in several ways. The first case is if you explicitly provide the type of the variable when declaring it. In this case, it corresponds to C# declaration that contains the type name:

// Visual Basic
Dim num as Integer
// C#
int num;

The second case is if you use an initialization expression, but don't specify the type explicitly. In this case, VB (at least recent versions) use type inference to deduce the type. This corresponds to C# var keyword:

// Visual Basic
Dim str = "Hello world"
// C#
var str = "Hello world"

Finally, there is a third case - you can declare Visual Basic variable without giving any type and without providing the initialization expression (at least with Option Strict turned off). In this case, VB.NET declares the variable as a value of type Object, but also allows you to invoke any methods of the object. This is quite close to what C# 4.0 does with the new dynamic keyword:

// Visual Basic
Dim sth
sth = new Random()
sth.Next()

// C#
dynamic sth;
sth = new Random();
sth.Next();

[EDIT]
Finally, the last case of use Dim in Visual Basic is to declare arrays. In C#, arrays are treated as just another data type, so the declaration is similar to what I wrote earlier for integers/strings:

// Visual Basic
Dim ints(10, 10) as Integer
// C# - we need to initialize the array if size is specified
int[,] ints = new int[10, 10]; 
// C# - we can use type inference too
var ints = new int[10, 10]; 

[/EDIT]

(I'm not really a VB expert, but I think these are the three possible cases. Please correct me if I'm wrong!)


The similar equivalent would be like

object Obj;

or any other datatype such as

int myInt;
string myString;

var could also be used as implicitly typed. Others would be explicitly typed. var was introduced for LINQ queries though.


Dim is a keyword used for declaration of variables used in VB.NET. I don't know about any usage in C#. I would say its (rough) equivalent would be the var keyword, provided that we are talking about VB 9 and C# 3. But I think it's useless to go into fights about details before we know what is the intended question anyway ;)


I can suggest Learning C# 3.0 (and later, maybe C# 3.0 in a Nutshell and C# in Depth) to learn C#. It's probably a good idea to learn the language fundamentals and start "thinking in C#", instead of trying to "translate" or "convert" your thoughts from VB to C# (or any other language, for that matter).


Edit: That said; you do want to compare the new language you're learning to the one(s) you already know whilst you go along, to learn the differences between the languages. In my opinion, when learning a new language, it's more about grasping the principles and fundamental differences :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜