You’re convinced you want to convert to C#, but where do you begin?  There’s a lot of confusion and frustration floating around on the message boards about the approach to take and which one is best.

Though I am the author of a successful VB.Net to C# commercial tool, I’m going to step outside myself and give an honest assessment of the options (including a plug for VBConversions’ only real competitor).  Hopefully this will help save you a lot of frustration in converting your code.

You basically have three choices:

  • Do it by hand
  • Use a free online converter
  • Use a commercial tool

Necessary Background

HowHardCanItBe
First, VB.Net to C# conversions are hard.  Really hard.  Absolutely everyone thinks at first that it is an easy task.  VBConversions was started in 2003 and I thought “how difficult can this be?”.  A few semicolons and regular expressions and you’re almost there, right?  Good thing I didn’t know then what I know now or I might not have started.

VB.Net to C# conversions can’t be done with a simple syntax only translator.  It has to completely understand every line of code and every variable, its definition, and all its references.  For this reason, online converters suck by their very architecture, as they don’t have visibility to the project Includes and references.  VBConversions also has a snippet converter and for the same reasons it isn’t nearly as good as the VBConversions project converter (though it’s better than the online free ones).

Some reasons why VB.Net to C# conversion is harder than you think:

  • VB.Net is case insensitive, whereas C# is case sensitive.  Every variable reference has to be identified and the casing made consistent.
  • There are many ambiguities in VB.Net, the most common being parenthesis.  Consider something simple like a(b): is it a function call, an array, or a default property.  It gets converted three different ways depending on the answer.
  • There are differences in operator precedence.
  • Event handling is very different between VB.Net and C#.
  • There is a lot of type conversions which have to happen when converting VB.Net to C#, which require the converter to have an in depth knowledge of each expression and its type.

This is just the easy stuff.  Add variable scoping differences, On Error Gotos, LINQ, embedded XML (with code holes!), Lambda expressions, implicit line continuation, and a hundred other things and it gets real complicated real quick.

Option 1 – Convert By Hand

Often people will start converting their VB.Net code to C# by hand, then think “I can write some quick tools to speed this up!”.  Add semicolons and maybe use a few regular expressions, right?  This exercise invariably leads to frustration and the pursuit of free or commercial tools.

It took six months to initially develop VBConversions, and its initial release in 2004 was terrible.  It wasn’t until two years later than I quit getting “you suck compared to tool “X” emails and started driving the other competitors at the time out of the market.

You can certainly write your own converter, as I did, but it will take you months (at least!) to get a viable product, and then you should be in the business of marketing converters, not just converting your own code.

As bad as they are, you would be better off using a free online converter and converting a page or two at a time, then spending half an hour in-between each conversion fixing the mistakes (not that this is actually a good idea).

PleaseDontDoIt

Option 2 – Use a Free Online Converter

WellThatWasFun
I did a Google search for “Free Online VB.Net to C# Converter” and picked the first five which showed up.  Then I converted the following very simple five lines of code.  The code wasn’t intended to trick the converters, just show very common scenarios of type conversions or array vs. function references.  It isn’t even the hard stuff.

codesample

This should be a pretty straightforward conversion, right?  The converted C# code should be something like:

codesample_cs

It converts the integer variable to a string before concatenating, and uses brackets on the array references.  This comes from the VBConversions snippet converter.

I tested the above section of code on the first 4 online converters I found on Google.  Not one of them converted the code correctly (and this is the super-simple stuff!).

Online converter #1:

online_converter_1

The integer variable wasn’t converted to a string, the array item wasn’t referenced with [], and for a bonus the inline comments were moved to their own line.

  • Convert integer to string – Fail
  • Array declaration – Success
  • Array assignment – Fail
  • Inline comments on separate line – Unanticipated Fail

Score = -2

Online converter #2:

online_converter_2

The integer variable wasn’t converted to a string, and the inline comments were again moved to their own line.

  • Convert integer to string – Fail
  • Array declaration – Success
  • Array assignment – Success
  • Inline comments on separate line – Unanticipated Fail

Score = 0

Online converter #3:

online_converter_3

Same as #2, the integer variable wasn’t converted to a string, and the inline comments were again moved to their own line.

  • Convert integer to string – Fail
  • Array declaration – Success
  • Array assignment – Success
  • Inline comments on separate line – Unanticipated Fail

Score = 0

Online converter #4:

online_converter_4

Wow – this one was really awful!  The integer variable wasn’t converted to a string, but there are unnecessary parenthesis around the expression as a bonus.  The array was declared as 2-dimensional, the array assignment didn’t have [], and the inline comments were moved to their own line.

  • Convert integer to string – Fail
  • Array declaration – Fail
  • Array assignment – Fail
  • Inline comments on separate line – Unanticipated Fail

Score = -4

Online converter #5:

online_converter_5

The best one of the group.  The integer variable wasn’t converted to a string (which is actually a big deal when converting a lot of code), but everything else converted correctly.

  • Convert integer to string – Fail
  • Array declaration – Success
  • Array assignment – Success
  • Inline comments on separate line – Success

Score = 2

In short, free online converters suck.  They are prevented architecturally from doing accurate conversions, but even with the knowledge they do have of the VB.Net code, in practice they are really, really bad.  VBConversions has a snippet converter, and although it is much better than the online converters, I actually hate it, and a warning is prominently displayed that you should use the project converter for more accurate results.

Option 3 – Use a Commercial Converter

When VBConversions was started in 2003, there were about 4 commercial VB.Net to C# converters available – and they were awful.  I was a full time VB.Net developer and saw that C# was definitely the place to be.  After seeing the available conversion tools, I decided to write my own.  Most of the other converters didn’t survive the VB.Net 2005 release, and couldn’t adapt to generics and other enhancements.  They all went out of business or died out.

Later Instant C# emerged as the only real competitor and we have been battling it out for years (yes, I’m actually giving a free plug for my competitor).  That we both have been able to consistently sell conversion tools in spite of the online free converters tells you something about their quality. Save yourself a lot of pain and frustration and get a real conversion tool – they’re both less than $200.  For the love of God, don’t use an online converter, even if you buy from my competitor.

We both offer trial editions which convert 2000 lines of code for free.  Evaluate both and make your own decision.

Instant C# – $179

VBConversions – $199

DoIt