VB 在 C# 中的“Dim”语句的等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2127738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the equivalent of VB's "Dim" statement in C#?
提问by nubela
Picking up C#, can't seem to find any useful reference to this, other than examples.
拿起 C#,除了示例之外,似乎找不到任何有用的参考。
So, what is Dim in C#?
那么,C# 中的 Dim 是什么?
采纳答案by John Feminella
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
.
在 VB 中,Dim
声明一个特定类型的变量(或变量类型,如果您没有指定)。如果是Dim x as Foo
,则声明了一个Foo
名为x
.
In C#, the equivalent is to state the type followed by the variable's name, as in:
在 C# 中,等效的是声明类型后跟变量名称,如下所示:
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:
C# 支持类型推断,因此您还可以执行以下操作:
// 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#:
Dim
在 VB 中没有相应类型的A类似于Object
在 C# 中声明类型:
object o = ...; // An object instance can hold any value or reference type.
回答by nubela
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.
var
也可以用作隐式类型。其他人将被明确键入。var
虽然是为 LINQ 查询引入的。
回答by Jarrett Meyer
Assuming that you mean Dim
in VB, C# uses either var
or the name of the type to declare a new type
假设您的意思是Dim
在 VB 中,C# 使用var
或 类型的名称来声明新类型
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.
请注意,var
关键字不是变体类型,就像在 VB 中一样。它是一个隐式类型的变量。
回答by Tomas Vana
Dimis 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 varkeyword, 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 ;)
Dim是用于声明 VB.NET 中使用的变量的关键字。我不知道 C# 中的任何用法。我会说它的(粗略的)等价物是var关键字,前提是我们正在谈论 VB 9 和 C# 3。但我认为在我们知道预期的问题是什么之前,讨论细节是没有用的;)
回答by Leif
I can suggest Learning C# 3.0(and later, maybe C# 3.0 in a Nutshelland 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).
我可以建议学习 C# 3.0(以及更高版本的C# 3.0 in a Nutshell和C# in Depth)来学习 C#。学习语言基础知识并开始“用 C# 思考”可能是一个好主意,而不是试图“翻译”或“转换”你的想法从 VB 到 C#(或任何其他语言,就此而言)。
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 :)
编辑:那说;您确实想将您正在学习的新语言与您在学习过程中已经知道的语言进行比较,以了解语言之间的差异。在我看来,在学习一门新语言时,更多的是掌握原理和根本区别:)
回答by Tomas Petricek
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:
这实际上有点棘手,因为Dim
可以以多种方式使用。第一种情况是,如果您在声明变量时明确提供了变量的类型。在这种情况下,它对应于包含类型名称的 C# 声明:
// 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:
第二种情况是如果您使用初始化表达式,但没有明确指定类型。在这种情况下,VB(至少最近的版本)使用类型推断来推断类型。这对应于 C#var
关键字:
// 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 变量而无需提供任何类型且无需提供初始化表达式(至少在Option Strict
关闭时)。在这种情况下,VB.NET 将变量声明为 type 的值Object
,但也允许您调用对象的任何方法。这与 C# 4.0 对 newdynamic
关键字所做的非常接近:
// 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:
[编辑]
最后,Dim
在 Visual Basic中使用的最后一种情况是声明数组。在 C# 中,数组被视为另一种数据类型,因此声明类似于我之前为整数/字符串编写的声明:
// 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!)
(我不是真正的VB专家,但我认为这是三种可能的情况。如果我错了,请纠正我!)