Meaning of signs in C#

Asked

Viewed 81 times

-1

What are the signs < >, [ ], { }, ( ) playing games in Unity?

  • I suggest you take a look at programming logic

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

Alone do not serve much, you need to understand the contexts of each.

Important to say that this has nothing to do with Unity which is only a library, I will answer about C#.

All these are delimiters as well as '' and "" are character and text delimiters. They use the same character to start and finish the text. Others delimit differently.

Many of them have functions and mean different things depending on the context being used.

<> defines information on parameterization of Generics. You can start studying in What is the difference between "Generics" (Java/C#) and "template" (C++) and What are the differences between Generic Types in C# and Java?. Then I will speak of another pair. They (called Angle Brackets) function as if they were the parentheses of a function, so there are parameters, but there it is for generic types instead of variables.

[] is used as a type indicator array or operator of a data collection where inside can go the index of access to the desired element (may be a little different from that, but it is not worth going into detail here). This operator can be overloaded on each type. When used only to indicate a array within them may be the size of it.

Another context it is used to delimit custom attributes.

Is called Brackets (square brackets).

{} is called braces (keys).

Can be used to delimit blocks of code in various shapes. Whenever you need to have several statements being treated as a single thing need to use them. It can be a block to join actions that must be executed conditionally after a if, else, or a repetition like while, for, foreach, or in a try or catch or finally. You can even create a loose block just to create scope even if it is not associated with such a command.

It also serves to group commands of a function, a class or other type of data or even a namespace. In some of them you only have to use if you have more than one line in the block.

It can also be used to initialize some types of data collections such as arrays, dictionaries, lists and any other that has an ability to add elements. It also serves as object initializers.

() is mainly the expression grouping operator. It equals math, you know? Except that in mathematics it has brackets and keys also to give a differentiation and a certain importance between them, in programming we do not differentiate this, putting parentheses within parentheses, the most internal are the most previous. In C# you cannot overload them.

It can also be used as a function parameter delimiter, even if it has no parameters it needs to have an empty pair to indicate that it is a function. The same goes for the call where you pass arguments that should be between them or if you don’t have any arguments they should be empty.

Another context it is used to define tuples, which are structures composed of 0 or more data of heterogeneous types, it is as if it were a struct but more simply and without having a type named explicitly. It has examples at the end of my reply.

Have I forgotten any? It may be that after I have written it some of them are used in other contexts.

Are you confused by all this? Perhaps the most recommended is to learn everything in a more structured way, and to see these symbols used in their proper contexts, with more detailed explanations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.