VB.NET 和 C# CIL 看起来一样吗?
当我构建 .NET 应用程序时,程序集将包含什么? CIL? 第二个问题,当用不同的 .NET 语言做同样的事情时,CIL 会完全相同还是在相似性方面相同且工作方式完全相同? 谢谢
When I build my .NET application, what will the assembly contain? CIL?
And second question, when doing the same thing in different .NET langugaes, will the CIL be exactly the same or same in the terms of similarity that works exactly the same way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,它们不会一样。
VB.Net 使用 Microsoft.VisualBasic.dll 中的辅助方法而不是许多 IL 指令来实现 VB 语义。
这包括相等(
"" = Nothing
)、某些字符串操作,我不记得还有什么。No, they won't be the same.
VB.Net uses helper methods in Microsoft.VisualBasic.dll instead of many IL instructions to achieve VB semantics.
This includes equality (
"" = Nothing
), certain string operations, and I don't remember what else.您不能保证 VB.Net 和 C# 将归结为相同的 IL,但如果您编写了功能相同的例程,则 IL 可以产生相同的结果。
You can not guarantee that VB.Net and C# will boil down to the same IL, but provided you wrote functionally identical routines, the IL could produce the same result.
对于许多方法,编译结果 (CIL) 将是相同的。
然而,这两种语言中都有一些结构的解决方式不同,或者根本不可能。这永远不会产生相同的指令。
For many methods, the compiled result (CIL) will be the same.
There are however constructs in either language that are solved differently, or just not possible. This can never result in the same instructions.
所有基于 .net 的语言都编译为 ILCode(中间语言)。如果执行相同的操作,则生成的 ILCode 将相同,因此,例如,对象上的成员访问将始终相同。但是,有些功能在其他语言中没有表示形式,因此 ILCode 可能无法用该语言表达,因为它使用了该语言中不可用的功能。不过,仍然可以引用程序集。
All languages based on .net compile to ILCode (Intermidiate Language). The resulting ILCode will be the same if done the same, so for example, member access on an object will always be the same. However, there are features that don't have representations in other languages, so it is possible that ILCode can't be expressed in that language, as it uses a feature not available in that language. Referencing the assembly is still possible though.
如果代码以相同的方式工作,那么除非出现编译器错误,否则您可以保证生成的 IL 将以相同的方式工作。
然而,这取决于编译器如何实现您的代码,并且由于 VB 和 C# 使用不同的编译器,因此不能保证它们会将您的代码实现为相同的 IL。
If the code works the same way, then barring compiler errors you can guarantee the IL generated will work the same way.
However, it's up to the compiler how it implements your code, and as VB and C# use different compilers there's no guarantee they'll have implemented your code as the same IL.
1) 是的,所有 .NET 应用程序都编译为 CIL
2) ,不,不同 .NET 语言之间的 IL 并不总是看起来完全相同。对于类似的源代码结构,它们可能是相似的,但每种语言在实现方式上都有细微的差异,因此 IL 最终看起来略有不同(但通常以相对相似的方式表现)。
1) Yes, all .NET applications are compiled to CIL
2), No, the IL won't always look exactly the same between different .NET language. They'll likely be similar for similar source code constructs, but each language has subtle differences in how things are implemented so the IL ends up looking slightly different (but often behaving in a relatively similar fashion).