这两个代码是否相同? [英] Are these 2 pieces of code the same?

查看:76
本文介绍了这两个代码是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VB.Net中制作了许多小程序.当然,我必须声明变量.我声明"New"变量的方式是

I make a lot of small programs in VB.Net. Of course, I have to declare variables. The way I declare "New" variables is

Public House As Building = New Building("live")


就是这个吗?


Is that the same as this?

Public House As New Building("live")


如果不是,什么更快,更重要,什么更安全?

顺便说一句,我在输入此字词时提出了自己的想法.


If not, what''s faster, does it matter, what is safer?

Btw, I made the house idea while I was typing this.

推荐答案

让我们看看
Let us see
Sub Main
	Dim House As Building = New Building("live")
	Dim House2 As New Building("live")
End Sub

Public class Building
	Public sub New (Name as string)
		
	end sub
End class


上面代码生成的IL是


The IL generated for the above code is

IL_0001:  ldstr       "live"
IL_0006:  newobj      UserQuery+Building..ctor
IL_000B:  stloc.0
IL_000C:  ldstr       "live"
IL_0011:  newobj      UserQuery+Building..ctor
IL_0016:  stloc.1

Building..ctor:
IL_0000:  nop
IL_0001:  ldarg.0
IL_0002:  call        System.Object..ctor
IL_0007:  nop
IL_0008:  nop
IL_0009:  ret



从上面可以明显看出,两种情况下的IL都是相同的.因此,以任何一种方式声明都没有区别.
我用Dim代替public,因为方法内不允许使用public.



From the above it is clear that the IL is same in both cases. Hence, there is no difference in declaring in either way.
I used Dim instead of public as public is not allowed inside a method.


我不认为可以有任何区别,但是我不知道要问什么.我从未使用Visual Studio安装对VB.NET项目的支持,也没有足够的懒惰来通过命令行使用始终可用的编译器对其进行测试,否则,我将只编译这两种情况,通过反汇编器ILDASM.EXE将编译后的代码运行到IL中,然后比较输出.为什么还要问这样的事情?

请参阅:
http://msdn.microsoft.com/en-us/library/f7dy01k1%28v = vs.100%29.aspx [ ^ ].

—SA
I don''t think there can be any difference, but I don''t know what was to ask about. I never install support of VB.NET projects with Visual Studio and lazy enough to test it with always available compiler via a command line, otherwise I would simply compile both cases, run compiled code through the disassembler ILDASM.EXE into IL and compared the output. Why even asking things like that?

Please see:
http://msdn.microsoft.com/en-us/library/f7dy01k1%28v=vs.100%29.aspx[^].

—SA


这篇关于这两个代码是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆