Chapter 20: Razor Syntax

Razor – C# and VB Code Syntax — that is, how you actually write C# or VB.NET code when you’re inside a .cshtml or .vbhtml file.

This is the most important difference between the two languages in Razor, and it’s where most beginners get confused when they see both C# and VB examples on W3Schools.

I’m going to explain it like we’re sitting together with two open editors side-by-side: one showing C# Razor, one showing VB Razor — same functionality, different syntax.

1. Quick Reminder: Two Files, Two Languages

Language File Extension Code Block Syntax Most Used Today? Feeling Like
C# .cshtml @{ … } Yes – 90%+ JavaScript / Java / C++
VB.NET .vbhtml @Code … End Code No – legacy Visual Basic / Classic ASP

Microsoft recommendation (2026): Use C# for new projects — it’s the future, has better tooling, more jobs, and almost all modern .NET documentation/examples use C#.

But W3Schools shows both so you can compare — and some old companies still maintain VB Web Pages sites.

2. Side-by-Side Comparison – The Same Page in C# vs VB

Let’s build the exact same small page in both languages so you see the differences clearly.

Goal: Show server time, greet user, loop over a list, conditional message.

C# Version (Index.cshtml)

HTML

VB.NET Version (Index.vbhtml)

HTML

3. Key Syntax Differences – Table for Quick Reference

Feature C# (.cshtml) VB.NET (.vbhtml) Notes
Code block start/end @{ … } @Code … End Code VB is more verbose
Variable declaration var name = “value”; Dim name = “value” VB requires Dim
Statement terminator ; (semicolon) None C# needs ;
If statement if (…) { … } else { … } If … Then … Else … End If VB uses Then & End If
For Each loop @foreach (var x in list) { … } @For Each x In list … Next VB uses Next
String concatenation $”Hello {name}” or string.Format() “Hello ” & name VB uses &
Date/Time shortcut DateTime.Now Now VB has shortcut
Case sensitivity Yes (case-sensitive) No (case-insensitive) C# is stricter
Boolean literals true / false True / False VB capitalizes
Array/List creation new List<string> { “a”, “b” } New List(Of String) From { “a”, “b” } VB uses Of & From

4. Inline Expressions – Same in Both (Mostly)

Both languages use @ for inline output — very similar:

C#:

HTML

VB:

HTML

The expression inside @(…) is C# or VB syntax accordingly.

5. Teacher Advice (2026 Perspective)

  • Choose C# unless you have a legacy VB codebase or really love VB syntax.
  • C# Razor examples are everywhere in 2026 — Microsoft Learn, YouTube, GitHub, Stack Overflow.
  • VB Razor is still supported in ASP.NET Core (you can write .vbhtml), but almost nobody starts new projects with it.
  • Once you learn one — the concepts transfer perfectly (loops, ifs, variables, helpers are identical — only punctuation changes).

Summary – Blackboard Close

ASP.NET Razor – C# and VB Code Syntax means:

  • C# Razor → uses @{ … }, semicolons, curly braces, var, case-sensitive
  • VB Razor → uses @Code … End Code, Dim, If … Then … End If, For Each … Next, case-insensitive
  • Inline code (@expression) is almost the same in both
  • HTML markup is identical — only the embedded code blocks change

Want to practice?

  • Shall we convert a full login form from C# to VB together?
  • Or compare how @helper looks in C# vs VB?
  • Or move to modern ASP.NET Core Razor syntax (which is 99% C#)?

Just say the word — you’re doing fantastic! 🚀🇮🇳 Keep shining from Hyderabad! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *