C Sharp

C# Operators

It's helpful to think of operators according to their precedence. In this section, I'll describe the most commonly used operators in the order presented earlier in Table 10-1.

Primary Expression Operators

The first category of operators that we'll look are the primary expression operators. Because most of these operators are fairly basic, I'll just list them and give a short blurb about their function. I'll then explain the other less obvious operators a bit more thoroughly.

  • (x) This form of the parenthesis operator is used to control precedence, either in mathematical operations or in method calls.

  • x.y The "dot" operator is used to specify a member of a class or structure where x represents the containing entity and y represents the member.

  • f(x) This form of the parenthesis operator is used to list the arguments to a method.

  • a[x] Square brackets are used to index into an array. They are also used in conjunction with indexers where objects can be treated as arrays. For more information on indexers, refer to Chapter 7, "Properties, Arrays, and Indexers."

  • x++ Because there's a lot to talk about here, the increment operator is covered later in this chapter in "Increment Operators and Decrement Operators."

  • x-- The decrement operator is also covered later in this chapter.

  • new The new operator is used to instantiate objects from class definitions.