Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ( ) (parenthesis) and { } (curly brackets) in Razor

What is the difference between them both. I thought they were the same but today I realized that they are not.

Why this is correct

@(Model.WillAttend == true ?      "This will be an exciting party with you" :      "So sorry. You'll lose the beeer") 

and this is not:

@{Model.WillAttend == true ?      "This will be an exciting party with you" :      "So sorry. You'll lose the beeer"} 
like image 495
Ricardo Polo Jaramillo Avatar asked Mar 18 '12 05:03

Ricardo Polo Jaramillo


People also ask

What is the difference between curly braces and parentheses?

Parentheses are for separating citations or other asides from the body text. Brackets show changes within quoted material. Braces —sometimes known as curly brackets—are not typically used except in technical and mathematical writing.

What are {} brackets called?

Curly brackets {} Curly brackets, also known as braces or curly braces, are rarely used in formal writing and are more common in other fields such as science, math, and computing.

What are curly bracket used for?

Curly brackets are rarely used in prose and have no widely accepted use in formal writing, but may be used to mark words or sentences that should be taken as a group, to avoid confusion when other types of brackets are already in use, or for a special purpose specific to the publication (such as in a dictionary).

What is the difference between curly brackets and square brackets?

The curly brackets are used to denote a block of code in a function. So, say we need a function to calculate the standard error we might do this. The square brackets are used to subset vectors and data frames.


2 Answers

The paren is just an explicit expression, and you will notice that you do not need a semi-colon. The brackets are a code block, to be used just like any other piece of code. Expressions have their output submitted as part of the HTML, whereas code blocks do not.

Phil Haack actually wrote a good primer on some Razor syntax

like image 83
Justin Pihony Avatar answered Sep 18 '22 15:09

Justin Pihony


To the question: "why is the second one not valid?", in addition to what Betty and Justin say, the issues specific to what you show: inside curly braces you need your code to follow the normal syntax of c#, so you can't have just a loose "a==b?c:d", without assigning the result to something. And you need a semicolon. So you could say

@{string message = Model.WillAttend == true ?       "This will be an exciting party with you" :       "So sorry. You'll lose the beeer";} 
like image 43
Levin Magruder Avatar answered Sep 22 '22 15:09

Levin Magruder