I’ve just been playing with type inference. This is a new feature in VB.NET 9 and C# 3 (I think it is v3) where you can get the compiler to assign a type to a new variable automatically by using “common sense”. Note that this looks just the same as weak typing, but it actually isn’t – the variables will be forced to the correct type by the compiler automatically, and intellisense even works!
For example, if you have type inference turned on, you can do this…
Dim i = 0 ‘i is automatically of type Integer
Dim Numbers = {1,3,5,7,9}’Numbers is automatically of type Array(of Integer)
For Each Num in Numbers ‘Num is automatically of type Integer
Next
It might seem lazy, but isn’t that the whole point?
- Type Inference in .NET 2008 Part 1: What is Type Inference?
- Type Inference in .NET 2008 Part 2: Turning Type Inference On and Off
No related posts.











