KayleighArianna KayleighArianna - 7 months ago 29
C# Question

What is the meaning of the different types of brackets/parentheses/braces used in C#?

I am trying to grasp the meaning of the different types of brackets/parentheses/braces used in C# and what the rules are or purpose of using different types in different situations.

Currently I have no trouble using the brackets/parentheses/braces but I feel as though I use them on a case-by-case basis without really grasping "why" I am using them and I would like to get an understanding of this.

Just for example, these are instances where I would use the brackets/parentheses/braces:

if(Row.Cells[0].Value != null)
listThings = new List<thing>();
//More code here

I would really appreciate any kind of explanation as to the rules behind the brackets and what the pattern is behind its uses and how the brackets/parentheses/braces are interpreted by the program.

Thank you in advance for your time


Curly brackets {} are used to group statements. In your case, the then clause of a standard if - then statement is wrapped in {} to group the statements together.

Square brackets [] are used for arrays, indexers, and attributes.

cells[0] means "Cell with index of 0", which in a more practical sense would mean "first cell".

Parentheses () are used to specify casts or type conversions:

double x = 1234.7;
int a;
a = (int)x; // Cast double to int 

As well as invoking methods or delegates:


Edit: As mentioned by itsme86 in the comments, () are also used for iteration statements like for(), foreach(), etc, and namespace keywords like using(), etc.

Angle Brackets <> are used to specify a type argument.

listThings = new List<thing>(); specifies a list of type thing