I love Visual Basic.NET, but as a .NET developer I write code in C# too. In this serie of posts I’ll show you the main differences between VB.NET and C#. Today I will discuss the structure of classes.

Imports System

Namespace MarYor.VBtoCSharpVB
    
    Module MainModule

        Public Sub Main()
            Console.WriteLine(GetFormattedDate())
            Console.ReadLine()
        End Sub

        Private Function GetFormattedDate() As String
            Return Today.ToLongDateString()
        End Function

    End Module

End Namespace

In C#, every statement starts with a ‘{‘ and ends with a ‘}’. In VB.NET you don’t have to start a statement explicitly. The end of a code structure needs to be marked with End .... Examples are End Namespace, End Class, End Sub or End If.
In C# you need to mark the end of the line with a semicolon. This makes it possible to break a statement across multiple lines. In VB.NET however, you don’t need the mark the end of the statement, but you have to use an underscore (_) as line terminator character to break statements over multiple lines. A little different approach.

First thing to remember: get used to curly brackets, semicolons and case sensitive keywords or variables.

In the example above you can see that I specify the namespace MarYor.VBtoCSharpVB. This is the name of my VB.NET project. In VB.NET you find the concept of a root namespace. By default the name of the project is assigned to this attribute. You can find this setting in Project > YourProjectName properties … > Application > Root namespace. For sake of this demo I cleared this attribute and added it to my class definition. Normally, if your code doesn’t live in an other (nested) namespace, you can omit this in VB.NET. In C# the use a root namespace works slightly different; it is a good habit to always specify the namespace according to the root namespace.

VB.NET is, unlike C#, not case sensitive. I doesn’t make any difference when you use public sub Main() instead of Public Sub Main() for example. A rule of thumb for C#: use lower case keywords, names of variables etc.

using System;

namespace MarYor.VBtoCSharpCSharp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(GetFormattedDate());
            Console.ReadLine();
        }

        private static string GetFormattedDate()
        {
            return DateTime.Today.ToLongDateString();
        }
    }
}

Last but not least: Imports statements are using statements in C#. Shared methods or variables in VB.NET are exactly the same as static methods or variables in C#. Because a Module in VB.NET is actually a static class, it is not possible to decorate for example a methode with the keyword Shared. It is already by default static. Obviously, in a regular Class you can.