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?
相关问题
- 'System.Threading.ThreadAbortException' in
- how to use special characters like '<'
- Check if tie in count of lists using linq
- Trouble with OR in Sharepoint CAML
- C# to VB - How do I convert this anonymous method
相关文章
- vb.net 关于xps文件操作问题
- 想问问Linq中有没有根据条件选择要不要关联表。
- Why do I need a ToList() to avoid disposed context
- Checking for DBNull throws a StrongTypingException
- LINQ .Include() properties from sub-types in TPH i
- Using the typical get set properties in C#… with p
- Can a method chain be called LINQ?
- Get grouped comma separated values with linq
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 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 on Infer, but in a code base without Strict the original version is more likely.