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