在VB.Net中初始化对象变量

时间:2022-08-26 22:34:37

In VB.Net is there any difference between the following three ways of initialising object variables

在VB.Net中,以下三种初始化对象变量的方法之间存在任何差异

Method 1

Dim commandObject As SqlCommand 
commandObject = New SqlCommand("MadeUpCommand")

Method 2

Dim commandObject As SqlCommand = New SqlCommand("MadeUpCommand")

Method 3

Dim commandObject As New SqlCommand("MadeUpCommand")

Is one more efficient than the others, or are they effectively all the same?

一个人比其他人更有效率,还是他们实际上都是一样的?

3 个解决方案

#1


There is no difference in the generated IL between the 3 methods.

3种方法之间产生的IL没有差异。

#2


Methods 1 and 2 are effectively the same. Method 1 obviously declares the object in a seperate statement to the assignment, but if the 2 lines are next to each other in code then it is also effectively the same as methods 2 and 3. In this case I would always use method 3 as it is the must succint. As darin says, they all generate the same IL.

方法1和2实际上是相同的。方法1显然在赋值的单独语句中声明了对象,但是如果代码中的2行彼此相邻,那么它实际上也与方法2和3相同。在这种情况下,我总是使用方法3作为它是必须的succint。正如达林所说,它们都会产生相同的IL。

I would only use method 1 when the declaration and assignment are required to have different scope, e.g. the assignment is done within an If block and the value needs to be tested outside that block.

当声明和赋值要求具有不同的范围时,我只会使用方法1,例如赋值在If块内完成,值需要在该块之外进行测试。

#3


(2) and (3) are equivalent. I'd hope (1) gets optimised to be equivalent too (even if there are other local variables/instantiations)

(2)和(3)是等价的。我希望(1)得到优化也是等效的(即使有其他局部变量/实例)

Most coding standards I've seen suggest having the variable initialised at the point of declaration, and also having the point of declaration as close as possible to first use.

我见过的大多数编码标准建议在声明点初始化变量,并且尽可能接近首次使用声明点。

Of course, in VB.Net 2 and later, I'd prefer to see

当然,在VB.Net 2及更高版本中,我更愿意看到

    Using commandObject As New SqlCommand("MadeUpCommand")
        ' Etc.
    End Using

#1


There is no difference in the generated IL between the 3 methods.

3种方法之间产生的IL没有差异。

#2


Methods 1 and 2 are effectively the same. Method 1 obviously declares the object in a seperate statement to the assignment, but if the 2 lines are next to each other in code then it is also effectively the same as methods 2 and 3. In this case I would always use method 3 as it is the must succint. As darin says, they all generate the same IL.

方法1和2实际上是相同的。方法1显然在赋值的单独语句中声明了对象,但是如果代码中的2行彼此相邻,那么它实际上也与方法2和3相同。在这种情况下,我总是使用方法3作为它是必须的succint。正如达林所说,它们都会产生相同的IL。

I would only use method 1 when the declaration and assignment are required to have different scope, e.g. the assignment is done within an If block and the value needs to be tested outside that block.

当声明和赋值要求具有不同的范围时,我只会使用方法1,例如赋值在If块内完成,值需要在该块之外进行测试。

#3


(2) and (3) are equivalent. I'd hope (1) gets optimised to be equivalent too (even if there are other local variables/instantiations)

(2)和(3)是等价的。我希望(1)得到优化也是等效的(即使有其他局部变量/实例)

Most coding standards I've seen suggest having the variable initialised at the point of declaration, and also having the point of declaration as close as possible to first use.

我见过的大多数编码标准建议在声明点初始化变量,并且尽可能接近首次使用声明点。

Of course, in VB.Net 2 and later, I'd prefer to see

当然,在VB.Net 2及更高版本中,我更愿意看到

    Using commandObject As New SqlCommand("MadeUpCommand")
        ' Etc.
    End Using