DEV Community

DIVAKAR UPADHYAY
DIVAKAR UPADHYAY

Posted on Originally published at devladder.pages.dev

Operators, conditions and loops (VB.NET)

👉 Full lesson with practice questions on DevLadder (free, lesson 3 of 12 in the VB.NET: Visual Basic .NET from Basics to Advanced (with C# Comparison) course)

Purpose VB.NET C#
Equality = ==
Not equal <> !=
Logical and/or (short-circuit) AndAlso, OrElse &&, `\
Logical and/or (always both) {% raw %}And, Or &, `\
Not {% raw %}Not !
String concatenation & (preferred) or + +
Integer division / modulus \, Mod / (on ints), %
Power ^ Math.Pow
Reference equality Is, IsNot == on references / is null
Ternary If(cond, a, b) cond ? a : b
Null-coalescing If(x, fallback) x ?? fallback
If marks >= 75 Then
    Console.WriteLine("Distinction")
ElseIf marks >= 35 Then
    Console.WriteLine("Pass")
Else
    Console.WriteLine("Fail")
End If

Select Case day                          ' = C# switch
    Case DayOfWeek.Saturday, DayOfWeek.Sunday
        Console.WriteLine("Weekend")
    Case Else
        Console.WriteLine("Weekday")
End Select

Select Case score
    Case Is >= 90 : grade = "A"
    Case 75 To 89 : grade = "B"
    Case Else : grade = "C"
End Select

For i As Integer = 1 To 10 Step 2       ' 1,3,5,7,9 (bounds inclusive!)
    If i = 7 Then Exit For               ' = break
    If i = 3 Then Continue For           ' = continue
    Console.Write(i)
Next

For Each name As String In names
    Console.WriteLine(name)
Next

Do While attempts < 3                    ' also: Do Until, Loop While, Loop Until, While...End While
    attempts += 1
Loop
Enter fullscreen mode Exit fullscreen mode

Pitfall: If user IsNot Nothing And user.IsActive Then evaluates both sides and throws when user is Nothing, use AndAlso.

Interview questions on VB.NET

What is the difference between VB.NET and C#?

Both compile to the same IL and run on the same CLR with the same libraries, so performance is the same. Differences are syntax and features: VB.NET uses English-like keywords, is case-insensitive, line-based with End blocks, has XML literals, My namespace, WithEvents/Handles and optional late binding; C# uses C-style syntax, is case-sensitive and gets new language features every year while VB.NET has been feature-stable since 2020. Most new projects use C#; VB.NET is mainly maintained in legacy systems.

What do Option Strict, Option Explicit and Option Infer do?

Option Explicit On requires variables to be declared. Option Strict On disallows implicit narrowing conversions and late binding: catching type errors at compile time (strongly recommended). Option Infer On lets Dim x = 5 infer the type from the initializer. Set them per project (Compile settings) or per file at the top.

What is the difference between Sub and Function?

A Sub performs an action and returns no value (C# void method). A Function returns a value with Return (or by assigning to the function name in old code). Both can take ByVal/ByRef, Optional and ParamArray parameters.


Read the full lesson with progress tracking and the rest of the course on DevLadder: free step-by-step tutorials and interview questions for developers.

Next in the course: Sub vs Function, ByVal/ByRef, Optional and ParamArray →

Top comments (0)