Are the two ways of declaring variables shown below the same?
下面显示的两种声明变量的方式是否相同?
int aa= 0, bb= 0, cc= 0, dd= 0;
int aa= 0;
int bb= 0;
int cc= 0;
int dd= 0;
3 个解决方案
#1
1
YES
C# supports single statement can assign multiple local variables
C#支持单个语句可以分配多个局部变量
A single statement can assign multiple local variables. Languages derived from C-style syntax often allow you to use commas to declare multiple variables in one statement. The C# language allows this.
单个语句可以分配多个局部变量。从C风格语法派生的语言通常允许您使用逗号在一个语句中声明多个变量。 C#语言允许这样做。
From 8.5.1 Local variable declarations
从8.5.1局部变量声明
A local variable declaration that declares multiple variables is equivalent to multiple declarations of single variables with the same type. Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.
声明多个变量的局部变量声明等效于具有相同类型的单个变量的多个声明。此外,局部变量声明中的变量初始值设定项与声明后立即插入的赋值语句完全对应。
First ones IL
code;
第一个IL代码;
.locals init ([0] int32 aa,
[1] int32 bb,
[2] int32 cc,
[3] int32 dd)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.0
IL_0006: stloc.2
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ret
Second one IL
code;
第二个IL代码;
.maxstack 1
.locals init ([0] int32 aa,
[1] int32 bb,
[2] int32 cc,
[3] int32 dd)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.0
IL_0006: stloc.2
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ret
#2
2
Yes, this:
是的,这个:
int aa = 0, bb = 0, cc, dd;
is exactly the same as the following, more common and readable version:
与以下更常见和可读的版本完全相同:
int aa = 0;
int bb = 0;
int cc;
int dd;
And as Soner Gönül shows, they produce the same IL code.
正如SonerGönül所示,他们生成相同的IL代码。
But you asked a good question, because things can be more subtle than that.
但是你提出了一个很好的问题,因为事情可能比这更微妙。
Pointers
In unsafe C# you can work with pointers. A pointer to an integer is written as int*
. So when you want to declare three variables of type int*
, you might write this:
在不安全的C#中,您可以使用指针。指向整数的指针写为int *。所以当你想声明int *类型的三个变量时,你可能会这样写:
int* aa, bb, cc;
And that correctly represents your intentions:
这正确地代表了你的意图:
int* aa;
int* bb;
int* cc;
However, the syntax for pointers came from the much older language C, and if you declare three pointer variables in C like this:
但是,指针的语法来自更老的语言C,如果在C中声明三个指针变量,如下所示:
int* aa, bb, cc;
Then that means something subtly different, and you've actually written:
然后这意味着微妙的不同,你实际上写道:
int* aa;
int bb;
int cc;
And that's why C developers will write int *aa
instead, to show that only aa
is a pointer type, and the rest are normal integers.
这就是为什么C开发人员会编写int * aa来表示只有aa是指针类型,其余的是正常整数。
int *aa, bb, cc;
However, I discourage you from writing such a thing in C altogether: it is confusing.
但是,我不鼓励你完全在C中写这样的东西:它令人困惑。
Visual Basic and VB.Net
In Visual Basic.NET you can also declare variables on one line:
在Visual Basic.NET中,您还可以在一行上声明变量:
Dim aa, bb As Integer
And this is the same as:
这与以下相同:
Dim aa As Integer
Dim bb As Integer
But if you program in Visual Basic (VB.Net's predecessor), then this:
但是如果你在Visual Basic(VB.Net的前身)中编程,那么这个:
Dim aa, bb As Integer
Means:
手段:
Dim aa As Any 'aa Is Variant
Dim bb As Integer
#3
1
They are the same. I would argue the second option is probably easier to read once your names get a bit more descriptive.
他们是一样的。我认为,一旦你的名字变得更具描述性,第二个选项可能更容易阅读。
#1
1
YES
C# supports single statement can assign multiple local variables
C#支持单个语句可以分配多个局部变量
A single statement can assign multiple local variables. Languages derived from C-style syntax often allow you to use commas to declare multiple variables in one statement. The C# language allows this.
单个语句可以分配多个局部变量。从C风格语法派生的语言通常允许您使用逗号在一个语句中声明多个变量。 C#语言允许这样做。
From 8.5.1 Local variable declarations
从8.5.1局部变量声明
A local variable declaration that declares multiple variables is equivalent to multiple declarations of single variables with the same type. Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.
声明多个变量的局部变量声明等效于具有相同类型的单个变量的多个声明。此外,局部变量声明中的变量初始值设定项与声明后立即插入的赋值语句完全对应。
First ones IL
code;
第一个IL代码;
.locals init ([0] int32 aa,
[1] int32 bb,
[2] int32 cc,
[3] int32 dd)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.0
IL_0006: stloc.2
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ret
Second one IL
code;
第二个IL代码;
.maxstack 1
.locals init ([0] int32 aa,
[1] int32 bb,
[2] int32 cc,
[3] int32 dd)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.0
IL_0006: stloc.2
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ret
#2
2
Yes, this:
是的,这个:
int aa = 0, bb = 0, cc, dd;
is exactly the same as the following, more common and readable version:
与以下更常见和可读的版本完全相同:
int aa = 0;
int bb = 0;
int cc;
int dd;
And as Soner Gönül shows, they produce the same IL code.
正如SonerGönül所示,他们生成相同的IL代码。
But you asked a good question, because things can be more subtle than that.
但是你提出了一个很好的问题,因为事情可能比这更微妙。
Pointers
In unsafe C# you can work with pointers. A pointer to an integer is written as int*
. So when you want to declare three variables of type int*
, you might write this:
在不安全的C#中,您可以使用指针。指向整数的指针写为int *。所以当你想声明int *类型的三个变量时,你可能会这样写:
int* aa, bb, cc;
And that correctly represents your intentions:
这正确地代表了你的意图:
int* aa;
int* bb;
int* cc;
However, the syntax for pointers came from the much older language C, and if you declare three pointer variables in C like this:
但是,指针的语法来自更老的语言C,如果在C中声明三个指针变量,如下所示:
int* aa, bb, cc;
Then that means something subtly different, and you've actually written:
然后这意味着微妙的不同,你实际上写道:
int* aa;
int bb;
int cc;
And that's why C developers will write int *aa
instead, to show that only aa
is a pointer type, and the rest are normal integers.
这就是为什么C开发人员会编写int * aa来表示只有aa是指针类型,其余的是正常整数。
int *aa, bb, cc;
However, I discourage you from writing such a thing in C altogether: it is confusing.
但是,我不鼓励你完全在C中写这样的东西:它令人困惑。
Visual Basic and VB.Net
In Visual Basic.NET you can also declare variables on one line:
在Visual Basic.NET中,您还可以在一行上声明变量:
Dim aa, bb As Integer
And this is the same as:
这与以下相同:
Dim aa As Integer
Dim bb As Integer
But if you program in Visual Basic (VB.Net's predecessor), then this:
但是如果你在Visual Basic(VB.Net的前身)中编程,那么这个:
Dim aa, bb As Integer
Means:
手段:
Dim aa As Any 'aa Is Variant
Dim bb As Integer
#3
1
They are the same. I would argue the second option is probably easier to read once your names get a bit more descriptive.
他们是一样的。我认为,一旦你的名字变得更具描述性,第二个选项可能更容易阅读。