16

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

So, what is Dim in C#?

John Feminella
  • 303,634
  • 46
  • 339
  • 357
nubela
  • 1
  • 24
  • 75
  • 123
  • 1
    Are you asking what the C# equivalent of VB.net's Dim is? – spender Jan 24 '10 at 16:24
  • for that matter, what in the world does `Dim` stand for in VB.NET? I know what it's used for, but I still don't know what it's supposed to mean. – Ben McCormack Jan 24 '10 at 16:29
  • @Ben Dim is short for dimension – acheo Jan 24 '10 at 16:32
  • @spender: Yea, when I read this initially I thought he was saying that there was a Dim keyword in C# –  Jan 24 '10 at 16:32
  • 1
    @Ben McCormack: DIM is short for "dimension", as PeanutPower said, because its original use was creating arrays, e.g. `DIM foo(100) AS INTEGER`. This is coming from distant and fading QBasic memories, but I think the "AS INTEGER" was necessary because arrays didn't use the type-prefix characters ("%' for integer, '$' for string, etc.) that scalar variables used. As an extension, you could also write things like `DIM bar AS STRING`, with no array dimension, to create scalar variables that didn't need the prefix character (so you could later refer to `bar` instead of `$bar` in this case). – Wyzard Jan 24 '10 at 16:43
  • @Wyzard, thanks for the explanation. I'm always wondered about that :-). – Ben McCormack Jan 25 '10 at 01:05

6 Answers6

19

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.
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 1
    yes, but var was brought in mostly for LINQ and if you do `var MyVar = 500; MyVar = "Change to String";` You will get an error.. other languages don't have this restriction –  Jan 24 '10 at 16:25
  • The last is an example of an implicitly-typed variable; type inference is different. – jason Jan 24 '10 at 16:26
  • 1
    @Roboto: C# is largely statically typed. You can't do what you describe because the type of a variable isn't mutable: once you declare as a type, that's the way it stays. @Jason: Implicit typing can't happen without type inference. They're pretty intimately linked. Essentially, type inference is what happens on the "right side" so that the compiler can bind `i` to a particular type. – John Feminella Jan 24 '10 at 16:27
  • Everything's an object! There's no type inference I would think –  Jan 24 '10 at 16:31
  • 1
    @Roboto: That's not true. As a strongly and (mostly) statically typed language, type is very important in C#. There are lots of examples where the type is inferred. (I think you may be misunderstanding the definition of what type inference means -- have you checked out http://msdn.microsoft.com/en-us/library/bb384061.aspx ?) – John Feminella Jan 24 '10 at 16:33
  • 1
    @Roboto: There absolutely *is* type inference in C# 3, although usually that phrase is used to describe inferring type arguments for generic methods. `var` is *implicit typing* - it would be reasonable to call this a form of type inference. – Jon Skeet Jan 24 '10 at 16:37
  • 1
    @Roboto, it is not true that "everything's an object". C# and .NET also use structs, and structs are a category of "value types" (along with integers and other numbers). All objects are "reference types." This matters a lot in terms of memory management, as value types are allocated on the stack, and reference types are allocated from a heap. For a value type to be passed as a parameter or return value, it must be "boxed" so that it can be stored in the heap, and have a lifetime that is independent of the stack. – Cylon Cat Jan 24 '10 at 16:40
  • @John, I would say that C# is entirely a strongly typed language. Even in C# 4, which introduces dynamic typing, "dynamic" is in itself a "strong type" used for referencing dynamic data and methods. Within that context it is dynamic, but a variable typed as "dynamic" cannot then be used to refer to a static type. By use of implicit typing, generic, and type inferrence, C# is much more flexible about strong typing than most other strongly-typed languages, but it is still a strongly typed language. – Cylon Cat Jan 24 '10 at 16:44
  • @Cylon: I think you misread my quote: I said "C# is a strongly and (mostly) statically typed...", meaning that it's all strongly typed and mostly statically typed. The exception to "it's statically typed" is `dynamic`, which defers certain steps that would otherwise occur to runtime. – John Feminella Jan 24 '10 at 16:45
  • @John, I think the issue is how to treat variables declared as dynamic, and we really won't know that until we start working with it. (And perhaps you have already; I've got my hands full without going down that road for now.) My understanding is that "dynamic" is constrained to work only with dynamic languages and data. You could not, for instance, define a List and assign that to a dynamic variable. (And if I'm wrong about that, well, that's probably a good thing!) – Cylon Cat Jan 24 '10 at 21:57
  • @cylon cat: you're wrong. You can do all kinds of things with dynamic in C# 4 - even though it's primarily there for interop with dynamic languages. – Jon Skeet Jan 24 '10 at 22:45
  • @Jon, then I will look forward to learning much. At the moment, though, I need to learn to write a Domain Service Provider for WCF RIA Services, and that will keep me supplied with fun for a while. – Cylon Cat Jan 25 '10 at 03:24
16

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.

Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
8

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!)

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
0

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.

0

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 ;)

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
  • Downvoted; there is no equivalent of "Dim" in C#. In VB, the Dim keyword is used to declare a variable. In C#, this intent is implicit in using a type to begin a statement. The var keyword in C# can be used in place of a type name, but it is still a strong type, and it requires that the variable be initialized on the same statement. The type used for the variable is inferred from the type of data assigned to the variable. – Cylon Cat Jan 24 '10 at 16:33
  • 1
    You're absolutely right, the only thing that makes Dim **roughly** equivalent to var is that they are used to declare a variable in both languages. However, in version 9 of VB, turning on the Option Infer, you can make them behave in a very similar way. – Tomas Vana Jan 24 '10 at 16:43
0

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 :)

leifericf
  • 2,324
  • 3
  • 26
  • 37
  • 1
    I wouldn't recommend C# in Depth in terms of getting started. It's really aimed at people who already have a reasonable grasp of C# 1. – Jon Skeet Jan 24 '10 at 16:35
  • @Skeet: I agree. Guess I should have been more clear on that. The first one (Learning C# 3.0) was a great book to start with, at least I thought so. PS: I'm honored to have you comment on my answer ^^ – leifericf Jan 24 '10 at 16:45