If you work with the .NET Framework long enough, you may eventually find yourself tasked with converting one .NET language to another. There are hundreds of questions on Stack Overflow for converting C# to VB.NET, VB.NET to C#, and even C# to F# - and maybe thousands more that aren't tagged. But first, ask yourself...


Does it really need to be translated?

Kick your feet up for a minute (nice shoes) and consider whether or not that other code really needs to be translated, because you don't need to!

legs-762565_640

If it's a relatively small block of code, by all means just translate it and be done. A little upfront effort, and you'll have the undying gratitude of your team... or at least yourself when you need to revisit it six months later. But what if it's a third-party library from GitHub that's frequently updated, and you'd like to keep pulling in the latest changes? Every update shouldn't mean another several days of translation.

Or what if it's a legacy app that your company invested 10 years in, complete with hundreds of tests proving it works (or what if it has NO tests?? 😱 ) There may be thousands of hours worth of slight tweaks and bug fixes that you'll never fully incorporate into a rewrite, no matter how diligent you are. There's a time to refactor, but there's a time to leave things alone.

💡You can have several .NET languages in one solution, as long as they're separated by project.

That's right. It's been possible for a long time, but I'd bet those fancy blue shoes that even some experienced devs don't know it - it's easy to live entirely within a single language. So move that legacy VB.NET or C# code into its own project and reference it from the project you're working in. I wrote a quickie example (pictured at the top of this post - grab the source code) that has a C# project referencing F# and VB.NET projects.

All you need to do is open the project with the language you want to use, and add references to the projects that use the other .NET languages. Here's a C# project with references to F# and VB.NET projects:

netlanguagereferences

If you check out my sample app, you'll see the modules in the other two languages are treated by C# like any other C# static class.


Still bugging you? Try translating it automatically

So you gave it a solid minute or two, but the thought of dealing with multiple languages just makes you itch all over?

pexels-photo-145985

If it's small enough to not warrant a whole separate project, then you might start off by trying an online translation service. Here's a few, but unless it's a very simple piece of code you're likely to still have a few errors to resolve. Still, they might get you pretty far along.

If those don't work, here are some other tools. These ones need to be installed, and some of them cost money, but they'll almost certainly work more reliably. To use decompilers, you'll need to compile the source code you want to translate by pasteing it into Visual Studio, building it, and checking the bin folder for the DLL file. Then the tool can decompile it into the target language for you.

  • Telerik JustDecompile decompiles to C# or VB.NET (free)
  • ILSpy can decompile assemblies to C#. (free)
  • JetBrains dotPeek decompiles to C# as well. (free)
  • DevExtras .NET CodeReflect decompiles to C# or VB.NET. (free)
  • .NET Reflector can decompile assemblies into C# or VB.NET, so compile the source language and then decompile into the target language. ($95 - $195)
  • Tangible Software Solutions has converters for VB.NET to C#, C# to VB.NET, and C++ and Java too. ($119 - $499)
  • VBConversions converts VB.Net to C# and if the testimonials they post are honest, then it's pretty impressive... 110,000 lines of code converted perfectly? ($50/month - $500)

And if all else fails?

"And if automatic conversion fails, where then will you go? If the translation defeats you, will you risk a more dangerous road?" ~Saruman....... loosely interpreted

gandalf-caladhras

People seem to have mixed experiences with the automatic translators, at least the online ones. Although C# and VB.NET are closer in functionality than they have been in the past, sometimes things just don't translate nicely.

For example, I ran a small with...end with example through the Telerik converter:

Private Sub AddCustomer()
    Dim theCustomer As New Customer

    With theCustomer
        .Name = "Coho Vineyard"
        .URL = "http://www.cohovineyard.com/"
        .City = "Redmond"
    End With

    With theCustomer.Comments
        .Add("First comment.")
        .Add("Second comment.")
    End With
End Sub

Public Class Customer
    Public Property Name As String
    Public Property City As String
    Public Property URL As String

    Public Property Comments As New List(Of String)
End Class

The Telerik converter is a good one - by far, the most recommended from what I've seen. It produced this C# code, which is valid and compiles, but completely negates the obvious benefit of the with...end with block, which is to simplify making multiple assignments to the same object.

class SurroundingClass
{
    private void AddCustomer()
    {
        Customer theCustomer = new Customer();

        {
            var withBlock = theCustomer;
            withBlock.Name = "Coho Vineyard";
            withBlock.URL = "http://www.cohovineyard.com/";
            withBlock.City = "Redmond";
        }

        {
            var withBlock = theCustomer.Comments;
            withBlock.Add("First comment.");
            withBlock.Add("Second comment.");
        }
    }

    public class Customer
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string URL { get; set; }

        public List<string> Comments { get; set; } = new List<string>();
    }
}

It might as well have just assigned the values using theCustomer directly, or maybe created a couple shorter variable names that were equivalent to theCustomer like this:

private void AddCustomer()
{
	var theCustomer = new Customer();
		
	var c = theCustomer;
	c.Name = "Coho Vineyard";
	c.URL = "http://www.cohovineyard.com/";
	c.City = "Redmond";
		
	var co = c.Comments;
	co.Add("First comment.");
	co.Add("Second comment.");
}

At least the output would produce the same results - it's not always the case. Sometimes, there's no way to get around just digging in and manually translating the code. By all means, check page 2 of the Google results first (I'm not sure I've ever been that desperate), but then you may need to get your hands dirty.

Here's a couple books that might help. The second one is a few years old, but the first one looks intriguing.

And here's a few more links to documentation: