Lessons in this course (12)

12. Converting VB.NET to C# and migration strategy

Advanced★ Must knowLesson 12 of 12
VB.NETC#
Dim x As New List(Of Integer)var x = new List<int>();
If x Is Nothing Thenif (x is null)
For i = 0 To n - 1for (int i = 0; i < n; i++)
arr(0)arr[0]
Dim a(9) As Integer (10 items)var a = new int[10];
x \ y, x Mod yx / y (ints), x % y
"a" & "b""a" + "b"
AndAlso / OrElse&& / ||
DirectCast(o, T) / TryCast(o, T)(T)o / o as T
AddHandler e, AddressOf Me += M;
Handles btn.Clickwire btn.Click += ... (or markup)
Shared / Modulestatic / static class
MustInherit / Overridableabstract / virtual
On Error Resume Nextno equivalent — rewrite with try/catch
My.SettingsIOptions<T> / appsettings.json

Migration strategy for a legacy VB.NET app:

  1. Turn on Option Strict and fix warnings (finds hidden bugs first).
  2. Add tests around critical behaviour.
  3. Mixed-language solution: new code in C# class libraries referenced by the VB project (both compile to IL).
  4. Convert class libraries one by one with a converter, review by hand (Mid/InStr 1-based, integer division, Nothing semantics for value types, On Error).
  5. UI: Web Forms → ASP.NET Core MVC/Razor Pages or Blazor (rewrite page by page behind a reverse proxy — strangler pattern); WinForms VB can move to .NET 8/10 WinForms directly.
  6. Use .NET Upgrade Assistant for project files and package updates.