What I Learned Porting .NETpad to C# (Premium)

I decided to port .NETpad from Visual Basic to C# in order to get up to speed on the latter language as quickly as possible. My plan is to use the more sophisticated C# language in at least the next few projects, and I didn’t want to have to worry too much about learning the language at the same time as learning a new framework.

I’ve mentioned this work a few times, most recently in What I’ve Learned About C#, Visual Basic, Windows Forms, XAML, and WPF (Premium). But as I write this, my C# porting effort is just about done: I’m still using Windows Forms and the .NET Framework, so all I really needed to do was duplicate the .NETpad user interface with the Visual Studio designer and then rewrite my event handlers and custom methods in the new language. It was time-consuming and occasionally difficult, in part because of obscure language differences and in part because Visual Studio treats C# very differently than it does VB.

Let’s talk about the language differences first.

As you may know, C# is a far more sophisticated language than VB, and it’s more syntactically similar to Java and other C-like (or “curly braces”) languages. On the flipside, VB is simpler, easier to read (especially for beginners), and thus is easier to learn as well. And for their use in Windows Forms applications, the two languages are essentially identical, from a functional perspective. (I suspect the situation is similar in Windows Presentation Foundation (WPF) and other newer frameworks, but we’ll find out soon enough.) That’s why I used VB for this first project.

But because C# is the lingua franca of the Microsoft development world, one of the issues with using VB---and this undercuts my claim above that VB is “easier,” in some ways---is that most of Microsoft’s documentation only provides examples in C#. So as I was making .NETpad, I often had to translate C# into VB when referencing Microsoft’s documentation. It was worth doing and instructional in its own way. But this will be a hurdle for anyone that chooses VB today.

(And in recent years, VB and C# development, which had previously occurred in lockstep with each other, has forked, with C# typically getting new features first, or VB not getting some new features at all. This hasn’t had any major ramifications yet, but it adds to the notion that VB has sadly become something of a second-class language.)

Anyway. Let me provide a few examples.

Most simplistically, you’ll be dealing with curly braces (“{“ and “}”) to demark code blocks and semi-colons (“;”) to demark the end of each line of code when you use C#. Consider the following simple If-Then-Else block in VB:
If TextBox1.WordWrap = True Then
    TextBox1.WordWrap = False
Else
    TextBox1.WordWrap = True
End If
In C#, that same code block looks very similar:
if (textBox1.WordWrap == true)
{
   textBox1.WordWrap = false;           
}
else
{
   textBox1.Word...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC