This is the twentieth in a series of blog posts I’m doing on the upcoming VS 2010 and .NET 4 release.
[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu]
Scenario 1: Basic Type Inference
For example, notice below how VS 2010 provides statement completion for a string (because we assigned a string to the “foo” variable):
If we later assign a numeric value to “foo” the statement completion (after this assignment) automatically changes to provide intellisense for a number:
Scenario 2: Intellisense When Manipulating Browser Objects
For example, below we are using the browser’s window object to create a global variable named “bar”. Notice how we can now get intellisense (with correct type inference for a string) with VS 2010 when we later try and use it:
When we assign the “bar” variable as a number (instead of as a string) the VS 2010 intellisense engine correctly infers its type and modifies statement completion appropriately to be that of a number instead:
Scenario 3: Showing Off
Because VS 2010 is psudo-executing code within the editor, it is able to handle a bunch of scenarios (both practical and wacky) that you throw at it – and is still able to provide accurate type inference and intellisense.
For example, below we are using a for-loop and the browser’s window object to dynamically create and name multiple dynamic variables (bar1, bar2, bar3…bar9). Notice how the editor’s intellisense engine identifies and provides statement completion for them:
Because variables added via the browser’s window object are also global variables – they also now show up in the global variable intellisense drop-down as well:
Better yet – type inference is still fully supported. So if we assign a string to a dynamically named variable we will get type inference for a string. If we assign a number we’ll get type inference for a number.
Just for fun (and to show off!) we could adjust our for-loop to assign a string for even numbered variables (bar2, bar4, bar6, etc) and assign a number for odd numbered variables (bar1, bar3, bar5, etc):
Notice above how we get statement completion for a string for the “bar2” variable.
Notice below how for “bar1” we get statement completion for a number:
This isn’t just a cool party trick…
Hope this helps,