Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 36 ยท Interop and Migration

VB.NET and C# Side by Side

Compare VB.NET and C# syntax side by side - declarations, operators, loops, classes, properties, events, lambdas, LINQ, gotchas such as 1-based bounds and And versus AndAlso, with runnable C# equivalents.

VB.NET and C# are two front ends for the same platform. Both compile to the same intermediate language, use the same class library, and run on the same runtime. A VB project can reference a C# project and vice versa, and most code can be translated line by line. Knowing both is a career multiplier: you can read the vast majority of .NET examples online, which are written in C#.

The VB.NET compiler is not available on this site. The VB column below is read-only; the C# side runs on the site's .NET 10 runner. Output is shown only for the C# blocks.

Structure and basics

Idea VB.NET C#
Statement end End of line (_ to continue, implicit continuation in many places) ;
Block If ... Then ... End If { ... }
Comment ' comment // comment
Variable Dim n As Integer = 5 int n = 5;
Inferred type Dim n = 5 var n = 5;
Constant Const Max As Integer = 10 const int Max = 10;
Case sensitivity No Yes
String concat a & b a + b
Interpolation $"Hi {name}" $"Hi {name}"
Not equal <> !=
Null / no object Nothing null
This instance Me this
Base class MyBase base

Operators with traps

VB.NET C# Notes
And, Or &, ` `
AndAlso, OrElse &&, `
Not !
\ / on ints VB / always returns a Double; \ is integer division
Mod %
^ Math.Pow VB has an exponent operator
= == and = VB uses = for both assignment and comparison
Is, IsNot ==, != on references Reference comparison
TypeOf x Is T x is T
C# Short-circuit versus not, and integer division (C# equivalents)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Control flow

VB
' VB.NET
If score >= 90 Then
    grade = "A"
ElseIf score >= 80 Then
    grade = "B"
Else
    grade = "C"
End If

Select Case day
    Case "Sat", "Sun" : kind = "weekend"
    Case Else : kind = "weekday"
End Select

For i As Integer = 0 To 9 Step 2
    Console.WriteLine(i)
Next
For Each item In items : Next
Do While x < 10 : x += 1 : Loop
C#
// C#
if (score >= 90) grade = "A"; else if (score >= 80) grade = "B"; else grade = "C";

kind = day switch { "Sat" or "Sun" => "weekend", _ => "weekday" };

for (int i = 0; i <= 9; i += 2) Console.WriteLine(i);
foreach (var item in items) { }
while (x < 10) x++;

Two differences that bite: VB's For i = 0 To 9 is inclusive at the end and C#'s i < 10 is not, and VB's Select Case has no fall-through, so there is no break.

Classes, properties and events

VB
Public Class Person
    Public Property Name As String
    Public ReadOnly Property Age As Integer

    Public Event Renamed As EventHandler

    Public Sub New(name As String, age As Integer)
        Me.Name = name : Me.Age = age
    End Sub

    Public Overridable Function Greet() As String
        Return $"Hi, I'm {Name}"
    End Function
End Class

Public Class Student
    Inherits Person
    Public Overrides Function Greet() As String
        Return MyBase.Greet() & " (student)"
    End Function
End Class
C#
public class Person
{
    public string Name { get; set; } = "";
    public int Age { get; }
    public event EventHandler? Renamed;
    public Person(string name, int age) { Name = name; Age = age; }
    public virtual string Greet() => $"Hi, I'm {Name}";
}
public class Student : Person
{
    public Student(string n, int a) : base(n, a) { }
    public override string Greet() => base.Greet() + " (student)";
}
VB.NET keyword C# keyword
Inherits / Implements :
Overridable / Overrides virtual / override
MustInherit / MustOverride abstract
NotInheritable sealed
Shared static
Friend internal
Protected Friend protected internal
Module static class
ByRef / ByVal ref / (default)
Optional default parameter value
ParamArray params
Handles, WithEvents (no equivalent; subscribe with +=)
C# The class above, running as C#

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Arrays, lambdas and LINQ

VB
Dim a(4) As Integer                      ' 5 elements (upper bound!)
Dim f = Function(x As Integer) x * 2
Dim big = From n In nums Where n > 3 Select n
C#
int[] a = new int[5];                    // 5 elements (length)
Func<int, int> f = x => x * 2;
var big = from n in nums where n > 3 select n;
C# Length versus upper bound, lambdas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Things only one language has

VB-only: XML literals, the My namespace, Handles/WithEvents, late binding with Option Strict Off, Select Case with ranges (Case 1 To 5, Case Is > 10), With blocks, implicit line continuation, optional Of-style generics syntax.

C#-only: pattern matching and switch expressions, records, unsafe code and pointers, top-level programs, ref struct and Span features, and most newer language additions. Microsoft's own language-evolution focus has been C#; VB gets bug fixes and compatibility updates rather than new features. The practical effect: new libraries are documented in C#, so reading C# fluently matters even for a VB developer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which VB operator corresponds to C#'s &&?

    AndAlso short-circuits like &&; And evaluates both operands.
  2. 2. What is the VB result of 7 \ 2?

    The backslash is integer division.
  3. 3. Dim a(4) As Integer in VB corresponds to which C#?

    VB gives the upper bound, so 5 elements.
  4. 4. Which VB keyword matches C# static?

    Shared members belong to the type; a Module is like a static class.

Summary

  • Both languages compile to the same IL and use the same libraries.
  • Learn the trap list: AndAlso/OrElse, / versus \, upper-bound arrays, inclusive For, = for equality.
  • Keyword pairs (Inherits/:, Overrides/override, Shared/static) make translation mechanical.
  • Read C# even if you write VB: most documentation uses it.

Next: Interop: P/Invoke, COM and Mixed Solutions