Type Inference in .NET 2008 Part 1: What is Type Inference?

This entry is part of a series, Type Inference in .NET»

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?

Entries in this series:
  1. Type Inference in .NET 2008 Part 1: What is Type Inference?
  2. Type Inference in .NET 2008 Part 2: Turning Type Inference On and Off
Powered by Hackadelic Sliding Notes 1.6.5

No related posts.

Tags: , ,

Leave a Reply