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

Edited 2 Years Ago by iConqueror: Changed to more approiate title

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.

Comments
thankyou sir

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.

Comments
thankyou that was very helpful
This question has already been answered. Start a new discussion instead.