Code Focused

Hidden Improvements in Visual Basic 11

Visual Basic 11 contains many enhancements you may not have noticed.

In my January article "Visual Basic Improvements in Visual Studio 11," I presented the major enhancements to Visual Basic 11 that appear in the Visual Studio 11 Beta. Key improvements include the Async and Await keywords, Iterators, Call Hierarchy and the Global keyword.

In February, I attended the 2012 Microsoft Global MVP Summit in Redmond. At the event, Microsoft Visual Basic Program Manager Lucian Wischik presented a four page list of enhancements in Visual Basic 11. Here are a few of the best VB11 updates from his list, enhancements that you may not have noticed.

Async
MSTest and XUnit now support Async and Await in unit tests. Prior to this support, the test method did not wait for the Await method to complete, potentially executing the Assert method too soon. Now, the Async method will complete and the Assert behaves as expected.

Security is enhanced to ensure that any framework method that delay-executes a delegate, does so in the proper SecurityContext. The number of heap allocations is reduced, which benefits server scalability.

Compiler Switches
The compiler switch "/vbruntime:*" embeds core functionality from the Visual Basic Runtime Library into your assembly to provide useful Visual Basic functions without requiring a reference to the runtime library. This new functionality opens Visual Basic to target platforms that do not support the Visual Basic runtime like the Portable Library, XNA, Windows Phone and the Micro Framework. With this switch, any use of Visual Basic features outside the embedded core functionality will result in a compiler error.

The compiler switch "/platform: anycpu32bitpreferred" was added as a better alternative to the AnyCPU setting. Many developers change the default Target CPU from x86 to AnyCPU, thinking that will provide optimum performance in both x86 and x64 operating system (OS) environments. However, the performance penalty for running 64-bit code is rarely worth it. It's justified only when large memory space is truly needed. The vast majority of applications will run faster as 32-bit applications, even in a 64-bit OS. This new compiler switch favors the benefits of x86 on an Intel CPU, but still permits deployment to other CPUs such as ARM. Note that due to a bug in VB11, this option is not available in Visual Studio's Project Properties Compile tab Target CPU field, it can only be set with the vbc.exe command line compiler.

The compiler switch "/highentropyva" marks the assembly header with a flag instructing the OS to perform additional randomization of the memory layout of the executable, making it less vulnerable to some security attacks. This switch is on by default.

Optional Parameter Overloads
The following overloads are now legal in VB11. In VB10, they generate the error message shown in Figure 1.
  Private Sub f(x As Integer)  
           End Sub  Private Sub f(x As Integer, 
           Optional y As Integer = 0)  End Sub
Figure 1. Visual Studio 2010 overloads error removed in Visual Studio 11 Beta
Read-Only Types
Three read-only types were added to the .NET Framework for compatibility with WinRT applications: IReadOnlyCollecton(Of T), IReadOnlyList(Of T) and IReadOnlyDictionary(Of K,V). Sample code to initialize the three structures is as follows:

  Private Sub NewVB11Types()  
   'Initialize the ReadOnlyCollection from a mutable collection    
   Dim VSMAuthors As New List(Of String)(New String() {       
   "Brust", "Kunk", "Nasr", "Papa", "Steele", "Vogel"})    
   Dim VSMColumns As New Dictionary(Of String, String) From {        
   {"C# Corner", "bit.ly/HF0unQ"}, {"On VB", "bit.ly/HcVWmO"}}    
   Dim roc As New System.Collections.ObjectModel.ReadOnlyCollection(Of String)(VSMAuthors)    
   Dim rol As System.Collections.Generic.IReadOnlyList(Of String) = VSMAuthors    
Dim rod As System.Collections.Generic.IReadOnlyDictionary(Of String, String) = VSMColumns
End Sub
Type Inference for Return Array Literals
Visual Basic 11 extends type inference to array literals as shown in the following code. In Visual Studio 2010, the "Return {}" statement would have been typed as an Object array incompatible with the required return type of Integer array and generated the error shown in Figure 2. Under Visual Studio 11, this sample code builds fine.

  Function f(i As Integer) As Integer()      
   If i = 0 Then Return {}      
   Return {1, 2, 3}  End Function
Figure 2. Array type inference error in Visual Studio 2010
Bug Fixes
Nearly 200 bugs were fixed in Visual Studio 11, including many situations that caused Visual Studio to crash: error reporting, closing a project, referencing an obfuscated assembly, using NoPIA (type embedding) and huge string concatenation. Visual Studio 2010 was the first release of the WPF-based user interface, so these crash bugs were not unexpected, and they were quickly addressed.

How can you best report errors and enhancement suggestions to Microsoft to help improve future versions of Visual Studio and the .NET languages? If there is a crash, allow the Watson report of the crash to be sent to Microsoft. These reports are aggregated, and the ones that occur most frequently get automatically created as bugs.

Bugs are sometimes drawn from StackOverflow or from blogs, but this is not a reliable way to get Microsoft's attention. It's better to file your bug or enhancement idea on Microsoft Connect or the Visual Basic Language forum. These sites get the greatest attention from Microsoft.

Explore the Visual Basic 11 Beta
I've listed just some of the lesser known improvements in the Visual Basic 11 Beta. If you have not done so already, download your copy of the Windows 8 Consumer Preview and the Visual Studio 11 Beta. I'm sure you'll enjoy these enhancements and find even more language goodies than I've discussed here.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.