VB and C# enhancements in VS 2010

Choosing a programming language is a personal choice that each programmer gets to make. It is akin to choosing a flavor of ice cream - there are many great options out there, but your favorite flavor is a matter of personal preference.

In Visual Studio 2010, we’ve made several enhancements to our two most popular languages, Visual Basic and C#, to give programmers all the tools they need to build great software no matter which language they prefer.

Visual Basic

The Visual Basic team focused on adding productivity features to the language so developers can get more done in fewer lines of code. The most common customer request for Visual Basic is to remove the underscore (“_”) character when breaking a code statement across multiple lines in most cases. Visual Basic 10 introduces implicit line continuation, which removes the need for the underscore character in most cases.

 

Function Filter(

ByVal customers As List(Of Customer),

ByVal orderCount As Integer

)

Dimquery =

FromcIncustomers

Where c.Orders.Count >

orderCount

Selectc

Another new productivity feature is auto-implemented properties. With auto-implemented properties, lines of boiler-plate property implementation code can be replaced with simple one-line declarations. Previously, property declarations often looked like this:

Private _FavoriteFlavor As String = "Butter Pecan"

Property FavoriteFlavor() As String

Get

Return _FavoriteFlavor

End Get

Set(ByVal value As String)

_FavoriteFlavor = value

End Set

End Property

Private _FlavorList As New List(Of Flavor)

Property FlavorList() As List(Of Flavor)

Get

Return _FlavorList

End Get

Set(ByVal value As String)

_FlavorList = value

End Set

End Property

Now property declarations can be declared much more simply:

Property FavoriteFlavor As String = "Butter Pecan"

Property FlavorList As New List(Of Flavor)

Collection initializers and array literals are simpler as well. Collections can now be initialized when they’re declared, and the type of array literals is inferred by the compiler.

Dim toppings = New List(Of String) From

{

"sprinkles",

"chocolate chips",

"strawberries"

}

Dim cones = {"sugar cone", "waffle cone"} 'the type String() is inferred

Visual Basic 10.0 now has better support for lambdas. Lambdas can now contain expressions that don’t return a value, as the Sub keyword indicates below:

Array.ForEach(toppings, Sub(n) Console.WriteLine(n))

Sometimes you’d like to do more complex work inside a lambda declaration. Visual Basic 10.0 supports multiline lambdas. The compiler will infer parameter and return types where possible just like in regular lambdas.

Dim doubleDown = Function(n As String)

If n.StartsWith("s") Then

Return "extra " & n

Else

Return n

End If

End Function

Interoperating with dynamic language code such as Python and Ruby has become simpler in Visual Basic 10.0. For example, the following code snippet calls a method defined in a Python library “math.py”:

Dim mathLib As Object = python.UseFile("math.py")

Dim firstNumber = 44.2

Dim secondNumber = 9.5

mathLib.PowerOf(firstNumber, secondNumber)

C#

C# 4.0’s major themes are interoperability with dynamic programming paradigms and improved Office programmability. Dynamic lookup, a new feature in C# 4.0, allows you to use and manipulate an object from IronPython, IronRuby, JScript, the HTML DOM, or a standard .NET library in the same way, no matter where it came from. Language enhancements such as named and optional parameters and improved support for COM clients give C# developers who are working with Office APIs the same great experience that Visual Basic developers have enjoyed.

 

Adding the new dynamic keyword to your code allows its type to be resolved dynamically at runtime rather than statically at compile-time. This allows dynamic languages to expose their objects to C# in a way that feels natural to a C# programmer:

dynamic dynamicObject = GetDynamicObjectFromRuby();

dynamicObject.Foo(7);

dynamicObject.Property = "Property value";

dynamicObject[0] = "Indexed value";

Optional method parameters are familiar to Visual Basic and C++ programmers and are now available for C# programmers. Optional parameters are declared with a default value in the method signature, as follows:

private void CreateNewStudent(string name, int currentCredits = 0, int year = 1)

The method above can be called in any of the following ways:

   

CreateNewStudent("Chloe");

CreateNewStudent("Zoe", 16);

CreateNewStudent("Joey", 40, 2);

To omit the currentCredits parameter value but specify the year parameter, the new named arguments feature (highlighted) can be used. All of the following are also valid calls:

CreateNewStudent("Jill", year: 2);

CreateNewStudent(name: "Bill", currentCredits: 30, year: 2);

CreateNewStudent("Will", currentCredits: 4);

Named arguments are also a great way to write self-documenting calls to your existing methods, even if they don’t use optional parameters.

Learn More

Find out more about Visual Studio 2010’s language enhancements and download samples on the VB Futures site and the C# Futures site. To play with the new features, download and install Visual Studio Beta 1, then join the conversation.

 

Namaste!