What’s new in C# 4.0 and VB 10.0 ?

By binukumar on May 27, 2010

With the introduction of Visual Studio 2010, there are lots of refinements in its main supported programming languages like C# and VB.  Here we list some of them from both languages:

C# 4

1. Dynamic lookup: A new type has been introduced called dynamic.  The idea is that you can call any operation on the dynamic type you want, and the compiler won’t check it at compile-time but figure it out at runtime.  A dynamic object is assumed to support any operation at compile-time, and only at runtime will you get an error. The result of any dynamic operation is of type dynamic.

// Instantiation
dynamic intCount = 7;                                                         // implicit conversion
dynamic intCount  = GetObject();

// Operations on dynamic types
intCount .fldField = intCount .propProperty;           // getting and settings fields and properties
intCount[“one”] = intCount[“two”];                             // getting and setting thorugh indexers
int intTempCount = intCount  + 3;                                // calling operators

2. Named and optional parameters: Now declare optional parameters by simply providing a default value for it. And call parameters by name:,

// Definition of the method
public void Method(int intX, int intY = 1, int intZ = 2){}

// Using the optional params
Method(1);          // same as Method(1, 1, 2);
Method(1, 2);       // same as Method(1, 2, 2);
Method(1, 2, 3);

// Named params called, note arbitrary order and missing intY
Method(intZ: 1, intX: 2);

3. Improvements to COM interoperability: A bunch of changes to facilitate COM interopability has been introduced, like passing values to reference params. Unlike in C#, COM APIs use ref params simply as a way to pass value type and make no assumptions  about mutability. Thus, specifically for COM methods we are allowed to do this,

// For this COM method signature,
void Method(ref int intI, ref int intJ) { }

// Previously we would have to do this:
int intX = 0;        int intY = 0;
Method(intX, ref intY);

// Now we can pass in intY        Method(intX, intY);  // But note that changes to intX inside the method will be discarded.
// Changes to intY will be retained.

Some good C# 4 sample projects can be found at: http://code.msdn.microsoft.com/cs2010samples

VB 10

Visual Basic 10 has been structured for developers to 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.

Another new productivity feature is auto-implemented properties. With auto-implemented properties, multiple lines of property implementation code can be replaced with simple one-line declarations.

Previously, property declarations often looked like this:

Private _FavoriteFirm As String = “PIT Solutions”
Property FavoriteFirm() As String
Return _FavoriteFirm
End Get
Set(ByVal value As String)
_FavoriteFirm = value
End Set

End Property

Now property declarations can be done much more simply:

Property FavoriteFirm As String = “PIT Solutions”

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.

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)

Of course there are many more features. But here we considered only some commonly used ones in the development scenario. You can find the complete features list in the MSDN website.

Leave a Reply

Contact us!