Easing Animation with WPF and Silverlight
Microsoft must guarantee safety of browsers offered by Windows Update

Better Together – C# and Visual Basic

C# 3 had new language extensions that are practical for every kind of application. No matter what application you’re creating, property initializers, collection initializers, Lambda expressions, LINQ… can increase productivity no matter if a Windows- or Web-application, a library, service… is created. That’s different with C# 4. The new language extensions are extremely useful for integrating with dynamic languages, .NET reflection, COM interop… Not every program created should use the dynamic keyword!

Looking at the extensions of Visual Basic 2010 it becomes very clear that a goal with C# and Visual Basic is that one language gets the features of the other. C# 3 introduced auto-properties, collection initializers, Lambda expressions… that were missing with Visual Basic. Wait, Visual Basic also had Lambda expressions. However, it was not possible to create multiline Lambdas. That’s possible now. Visual Basic 2010 also has auto-implemented properties and collection initializers.

Auto-Implemented Properties C# 3.0
public string FirstName { get; set; }
  VB 2010
Public Property FirstName As String
Collection Initializers C# 3.0
var names = new List<string>() { "James", "Jack", "Jochen" };
  VB 2010
Dim names = New List (of String) From { "James", "Jack", "Jochen"}
Implicit Line Continuation   This was never an issue with C# because ; is used. With Visual Basic 2010 now _ can be omitted.
Multiline Lambda Expressions C# 3.0
Func<int, int> expr = x =>
return x + 7;
  VB 2010
Dim expr = Function(x)
Return x + 7
End Function

Features that already have been in Visual Basic are now available in C# as well:

Optional Parameters C# 4.0
public void CreateRacer(string firstName, string lastName, int starts = 0, int wins = 0)
Public Sub CreateRacer(firstName As String, lastName As String, Optional Starts As Integer = 0, Optional Wins As Integer = 0)
Named Parameters C# 4.0
CreateRacer("Michael", "Schumacher", starts : 251, wins : 91);

CreateRacer("Michael", "Schumacher", starts := 251, wins := 91)
Dynamic C# 4.0
dynamic c = GetCalculator();
dynamic result = c.Add(3, 4);

Dim c = GetCalculator()
Dim result = c.Add(3, 4)

Dynamic allows calling into dynamic scripting languages such as Python, Ruby, JavaScript, it gives an easy way to use .NET reflection, and allows using the COM automation interfaces. Together with optional and named parameters this is really handsome! With Visual Basic 2010 the internal bindings have changed as well for interop with dynamic binders. Just the syntax of Visual Basic stayed the same with Option strict off.

C# 4 and Visual Basic 2010 also offer new features that haven’t been available with both of these languages. Among them are type equivalence support that is extremely useful with COM interop. With the /link compiler option (this is the same with both languages), COM type information can be embedded so that PIA assemblies are no longer required. Co- and contra-variance for interfaces and delegates is supported by setting the out and in keywords.

C# and Visual Basic are getting together – more and more. There’s not a lot difference in the features of these languages. The yield statement is still missing in Visual Basic – or did I miss such an enhancement in Visual Basic?

Depending on the preference of curly brackets or writing more text, the language of choice can be selected. For myself, with a background in C++ of course I prefer C#, but that’s really just a personal taste.

More information on C# 4 in my new book Professional C# 4 with .NET 4 and in my Take off to .NET 4 workshop.



Feed You can follow this conversation by subscribing to the comment feed for this post.

The comments to this entry are closed.