I have been coding in c# on and off since I started my first year of the degree in IT two years ago. Of course you use the (), [] and {} brackets very frequently but I have never took the time to actually look where you apply each type of bracket.

This is what I know about these brackets so far:
- Curly brackets are used for opening and closing methods and classes and defining elements in arrays
- You can use square brackets to create arrays
- Paranthesis are used in method calls

So I have come up with a few questions:
1. What exactly is the difference between the paranthesis (), the square brackets [] and the curly brackets {}?
2. What purpose do each of these serve?
3. Where do we apply each of these brackets?

For an example you can create an array and fill it with elements:

string[] Meats = { "Roast Beef", "Salami", "Turkey", "Ham", "Pastrani" };

This uses the [] bracket to create the array and {} to define its elements.
Then you can reference a Random object and get a random number from the arrays length:

string randomMeat = Meats[Randomizer.Next(Meats.Length)];

This uses [] brackets and internal ().

From these two code samples you can see that:
- [] creates the array
- () contains a paramater of Meats.Length
- {} defines the elements in the array

So would this mean that in a simple form:
- [] is used to creates things?
- () is used to pass paramaters and call methods?
- {} is used to define things?

Could someone please shed some light on this for me.

Thankyou

Dani AI

Generated

Good instincts from and solid points from and — the three bracket types are simply different grammar tokens that the C# compiler uses to mean different things depending on context. The short practical takeaway: parentheses () indicate grouping/parameter lists and invocation, square brackets [] indicate indexing and attribute syntax, and curly braces {} indicate code blocks and initializers — but each has a few extra, commonly-missed uses and gotchas worth calling out.

Parentheses: used for method declarations/calls, control-statement conditions (if, for, while, switch, catch, using, lock), expression grouping, casts, and lambda parameter lists. A frequent confusion is “method reference vs method call” — the presence or absence of () matters: registering a handler requires the method group (no ()); adding () attempts an immediate call. Example:

void OnTick(object s, EventArgs e) { /* ... */ }
timer.Elapsed += OnTick;    // registers handler (method group)
timer.Elapsed += OnTick();  // wrong here — tries to call OnTick immediately

Square brackets: used for array and indexer access, multi-dimensional indexing (commas inside the brackets), and for attributes placed above types/members. In custom classes this[...] can declare indexers that accept one or more parameters. Attributes themselves use [] but may contain () for their constructor arguments.

Curly braces: define lexical scope (variables declared inside are local to the block), type/method/property bodies, and object/collection initializers. They also appear inside interpolated strings to mark expressions ($"Value: {expr}"). Common advice: always use braces for control structures (avoid single-line omission) and rely on an editor with bracket matching/auto-formatting to prevent mismatches and scope bugs. These notes extend the earlier replies and address the typical pitfalls that show up in real code.

Recommended Answers

All 2 Replies

It comes down to how the compiler reads and interprets your code. By defining different symbols for different things the compiler can infere what action you are trying to do. If you have done a course on compilers you would have used similar structures to tell your compiler how to handle certain lines in your code and branch appropriately.
But your final points are correct. Those structures do the things you describe.

commented: thankyou sir +1

It's different on a per language basis, but in most languages:

Braces {} are generally for object notation and grouping.
they define an object. Such as when you use them to define a class, enum, stuct ect.
Or for enclosing a memeber or code group, such as a method or property.

In your example:

string[] Meats = { "Roast Beef", "Salami", "Turkey", "Ham", "Pastrani" };

This is an object notation. It's not short hand specifically for creating an array, you can actually create any object this way.

public class myobject
    {
      public string Title {get; set;}

      public bool isInit {get; set;}
    }

    //create shorthand:
    myobject mo = new myobject(){Title="mytitle", isInit=false};

Brackets [] are indexers
when an object can be indexed, the [] supply a selector for the index.
When decaling a type, adding [] to the end is stating that you want an indexable array.
The only other time you will see brackets in C# is for flags. Sometimes when you define a class or interface you will want to flag the members for serialiaztion such as if you wanted to ignore a property in a class you would use the [XmlIgnore()] flag.

public class myobject
    {
       [XmlIgnore()]
       public bool ShipmentSpecified
       { get; set; }
    }

Parentheses () are used in math, casting, grouping, and calling.

Mainly () are for parameters. They are used to denote a situation where a parameter could be passed or recieved. This is most frequently when defining or calling a method.

Casting, or telling the compiler to expect a suggested type by passing in the type as a parameter before the data is always done with parentheses.
ex: int num = (int)5.123;

and of course math grouping ((1+2)/3)

So you certainly get the idea, but [] is not used to create 'things' it specifies arrays specifically. () is used in calling methods, which is part of creating 'things' (primitive types excluded) in conjunction with the 'new' keyword. {} define and group code into objects, methods, and properties, as well as used in notation for instances of objects.

commented: thankyou that was very helpful +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.