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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
@{ // C# code block - notice curly braces and semicolons var name = "Webliance"; var city = "Hyderabad"; var hour = DateTime.Now.Hour; var foods = new List<string> { "Biryani", "Haleem", "Irani Chai", "Osmania Biscuit" }; string greeting; if (hour < 12) { greeting = "Good Morning"; } else if (hour < 17) { greeting = "Good Afternoon"; } else { greeting = "Good Evening"; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Razor C# Example</title> </head> <body> <h1>@greeting, @name from @city! 🌟</h1> <p>Current server time: @DateTime.Now.ToString("hh:mm tt - dddd, dd MMMM yyyy")</p> <h3>Hyderabad Must-Try Foods:</h3> <ul> @foreach (var food in foods) { <li>@food.ToUpper()</li> } </ul> @if (foods.Count >= 4) { <p style="color: green; font-weight: bold;">Wow — full Hyderabadi menu today!</p> } </body> </html> |
VB.NET Version (Index.vbhtml)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@Code ' VB code block - notice no semicolons, Dim keyword, End If / End For Dim name = "Webliance" Dim city = "Hyderabad" Dim hour = Now.Hour Dim foods As New List(Of String) From { "Biryani", "Haleem", "Irani Chai", "Osmania Biscuit" } Dim greeting As String If hour < 12 Then greeting = "Good Morning" ElseIf hour < 17 Then greeting = "Good Afternoon" Else greeting = "Good Evening" End If End Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Razor VB Example</title> </head> <body> <h1>@greeting, @name from @city! 🌟</h1> <p>Current server time: @Now.ToString("hh:mm tt - dddd, dd MMMM yyyy")</p> <h3>Hyderabad Must-Try Foods:</h3> <ul> @For Each food In foods @<li>@food.ToUpper()</li> Next </ul> @If foods.Count >= 4 Then @<p style="color: green; font-weight: bold;">Wow — full Hyderabadi menu today!</p> End If </body> </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#:
|
0 1 2 3 4 5 6 |
<p>Price after tax: ₹@(price * 1.18)</p> |
VB:
|
0 1 2 3 4 5 6 |
<p>Price after tax: ₹@(price * 1.18)</p> |
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! 😊
