混合 VB.NET 的 Option Strict 和新的 Option Infer 指令的最佳方式是什么?
在相关问题中,我的团队即将(希望)开始使用 LINQ,并且我想利用匿名类型。 混合 VB.NET 的 Option Strict(我们在项目的整个生命周期中一直使用)和新的 Option Infer 指令的最佳方式是什么?
In a related question, my team is about to (hopefully) start using LINQ, and I'd like to take advantage of anonymous types. What is the best way to mix VB.NET's Option Strict (which we've been using through the life of the project) and the new Option Infer directives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Option Strict 和 Option Infer 不冲突,所以我认为没有什么坏处两者都在。
作为风格指南,我更喜欢将 Option Strict、Explicit 和 Infer 放在每个类文件的顶部 - 这可以防止项目或 IDE 设置的差异导致问题,并明确使用哪些设置。
Option Strict and Option Infer do not conflict, so I see no harm in having both on.
As a style guide, I prefer to put Option Strict, Explicit, and Infer at the top of each class file - this prevents differences in project or IDE settings from causing issues, and makes it clear what settings are used.
Option Strict 可以在没有 Option Infer 的情况下使用,但 Option Infer 不应在没有 Option Strict 的情况下使用,因为这可能会导致结果 IL 出现差异。
考虑这行代码:
使用 Option Strict Off 和 Option Infer Off 时,相当于:
If str="" then txtBox.Text 设置为 Nothing/空字符串。
当 Option Infer On 但 Option Strict Off 时,变成:
CDate(Nothing) = Date.MinValue and so txtBox.Text = "01/01/0001"
Option Strict 只能使你的代码无法编译,Option Infer 可以改变它的含义。 这并不是说 Infer 不是一件好事,一般来说它是好事,但是您需要注意一些注意事项。
原来的代码可以写成:
在这种情况下,如果你转向 Option,Option Strict 也救不了你。 Infer On,但在没有 Strict 的代码库中,原始版本更有可能。
Option Strict can be used without Option Infer, but Option Infer should not be used without Option Strict as that can lead to a difference in the resulting IL.
Consider this line of code:
With Option Strict Off and Option Infer Off, that is the equvalent of:
If str="" then txtBox.Text is set to Nothing/empty string.
With Option Infer On but Option Strict Off that becomes:
And CDate(Nothing) = Date.MinValue and so txtBox.Text = "01/01/0001"
Option Strict can only make your code not compile, Option Infer can change its meaning. That is not to say that Infer can’t be a good thing, in general it is, but there are a few caveats that you need to be aware of.
The original code could be written as:
In which case Option Strict won’t save you if you turn Option. Infer On, but in a code base without Strict the original version is more likely.