简体   繁体   中英

Will VB.NET and C# CIL look the same?

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

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可以产生相同的结果。

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.

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.

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) 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM