Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

VB.NET and C# Side by Side

Compare Visual Basic .NET and C# syntax for variables, conditions, loops, methods, classes and operators, so you can read both languages and use the runnable C# examples on this site.

VB.NET and C# are two front ends for the same platform. They compile to the same IL, use the same class library and run on the same runtime, so almost every concept you learn in one carries over to the other. What differs is the spelling. Since this site can run C# but not VB, knowing how to translate between them lets you use the C# stand-ins to explore every VB.NET idea in this course.

This lesson is a translation table with examples. Keep it open while you read the later chapters.

The big differences at a glance

Aspect VB.NET C#
Blocks If ... End If, Sub ... End Sub { ... }
Statement end End of line Semicolon ;
Case sensitivity Insensitive (Name = name) Sensitive
Comments ' comment // comment
Declare a variable Dim x As Integer = 5 int x = 5;
Assignment vs. equality = for both = assign, == compare
Not equal <> !=
Logical operators And, Or, Not, AndAlso, OrElse &, !, && and the pipe operators
String concat & +
Null Nothing null
Generics List(Of Integer) List<int>
This / base Me, MyBase this, base
Array bounds Dim a(4) = 5 elements (0..4) new int[5] = 5 elements
Entry point Sub Main static void Main or top-level statements

Two of these deserve extra care, because they bite when you translate.

Array bounds. In VB the number in Dim a(4) As Integer is the upper bound, so the array holds five elements with indexes 0 to 4. In C#, new int[4] creates four elements. Chapter 3 returns to this.

Short-circuiting. VB's plain And and Or evaluate both sides. AndAlso and OrElse stop early like C#'s && and ||. Prefer AndAlso and OrElse for conditions.

Variables and decisions

VB
Dim score As Integer = 82
Dim grade As String

If score >= 90 Then
    grade = "A"
ElseIf score >= 80 Then
    grade = "B"
Else
    grade = "C"
End If

Console.WriteLine($"Score {score} is grade {grade}")

The same logic in C#, which you can run:

C# Stand-in (C#): decisions

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.

Note ElseIf (one word) in VB versus else if (two words) in C#.

Loops

VB
For i As Integer = 1 To 5
    Console.Write(i & " ")
Next

For Each fruit As String In {"apple", "pear"}
    Console.WriteLine(fruit)
Next

Dim n As Integer = 3
Do While n > 0
    n -= 1
Loop

VB's For i = 1 To 5 includes the end value. C# needs for (int i = 1; i <= 5; i++). For Each becomes foreach, and Do While ... Loop becomes while.

C# Stand-in (C#): the same three loops

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.

Procedures

VB separates procedures that return nothing (Sub) from those that return a value (Function). C# uses void or a return type for both.

VB
Function Square(x As Integer) As Integer
    Return x * x
End Function

Sub PrintSquare(x As Integer)
    Console.WriteLine(Square(x))
End Sub
C# Stand-in (C#): Sub and Function

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.

The parameter syntax is reversed: VB writes the name first (x As Integer), C# writes the type first (int x).

Classes

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

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

    Public Overrides Function ToString() As String
        Return $"{Name} ({Age})"
    End Function
End Class

Dim p As New Person("Ada", 36)
Console.WriteLine(p)

Constructors are named New in VB, and overriding needs Overrides on the member. Inheritance is Inherits, interfaces are Implements, and visibility keywords are the same words (Public, Private, Protected, Friend instead of C#'s internal).

C# Stand-in (C#): the Person class

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.

Features that differ in spirit

Some things are present in only one language or work differently:

  • VB has XML literals, With blocks, Select Case ranges, Handles clauses for events, My namespace shortcuts (My.Computer, My.Settings) and late-bound Object calls with Option Strict Off.
  • C# has unsafe code and pointers, yield in iterator blocks written in a more explicit form, pattern matching with more forms, records, top-level statements, and many newer language features.
  • VB lacks some recent C# syntax (for example, newer pattern forms and primary constructors), while its dialect stays stable.

None of this stops you from mixing: a VB project can reference a C# library. The Interop and Migration chapter covers this.

How to translate without confusion

  1. Replace braces by the matching End keyword and put the type after the name.
  2. Swap == for =, != for <>, && for AndAlso, || for OrElse, ! for Not, null for Nothing.
  3. Replace + between strings with &.
  4. Turn List<int> into List(Of Integer).
  5. Re-check array sizes: C# size N becomes VB upper bound N-1.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

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

    AndAlso short-circuits like &&. Plain And evaluates both operands.
  2. 2. In VB, how many elements does Dim a(4) As Integer create?

    The number is the upper bound, so indexes 0 to 4 give five elements.
  3. 3. What is C#'s null in VB.NET?

    Nothing is the null reference (and also the default value for value types).
  4. 4. How is the generic type List<int> written in VB.NET?

    VB uses the (Of T) syntax for type arguments.
  5. 5. Which statement is true about the two languages?

    Both compile to IL and use the same class library, so they interoperate freely.

Summary

  • VB.NET and C# differ in spelling, not in capability: same runtime, same library, same IL.
  • Blocks use End ... instead of braces; types follow names; & joins strings; <>, AndAlso, OrElse, Nothing replace !=, &&, ||, null.
  • Array declarations use the upper bound in VB, which is one less than the C# size.
  • Use the translation rules to run the C# stand-ins and map them back to VB.

Next: Variables and Data Types