View Single Post
  #5 (permalink)  
Old March 31st, 2011, 04:49 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Just did some digging in the source code and indeed the example you're referring to is using type inference.

Consider this:

Code:
Dim query As Table(Of title) = dc.titles
For Each item In query
  Console.WriteLine("{0}: {1}",
     item.title_id,
     item.title)
Next
Here, item *is* a title instance and not an Object. That's why you get IntelliSense when you type item and then a dot. Additionally, you can see the type of the item variable by hovering your mouse over item in the line with For Each. You see a popup telling you that item is, in fact, a title instance.

Type inference is explained in Chapter 1, page 18 and further. Stuff like Option Explicit and Option Infer are described in detail there as well.

LINQ in .NET relies heavily on type inference, mainly because of the dynamic nature of queries and anonymous types.

So, if you want to hear "what other readers think about this issue", I'll say you're being a bit too harsh on this book and the code examples. The example you're referring to is technically correct, and shows best practices when it comes to type inference in queries. If you don't like the syntax of type inference, you can always add "As title" to the code to make it more explicit, but it's certainly not neded to write type-safe code.

Hope this helps.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; March 31st, 2011 at 04:51 AM..
The Following User Says Thank You to Imar For This Useful Post:
jminatel (March 31st, 2011)