Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
Operators, in C#, are symbols used within an expression or statement to specify the operations to be performed during evaluation of the expression. Operators are program elements that can be applied to one or more operands in an expression to perform computations. The operands used with the operator can be literals, fields, local variables and expressions.
In general, operators help in building expressions that form the primary means to work with data stored in constants and variables.
Although all the C# operators are provided with predefined implementation that are used in any expression containing built-in types, they can be overloaded to modify the behavior when applied to user-defined types. Operators such as checked and unchecked help in providing the option to handle or ignore exceptions related to overflow caused during arithmetic operations.
Like C++ operators, C# operators also have precedence and asociativity which determine the order of evaluation of operators in an expression. Based on the number of operands used with the operator, operators are classified as unary (single operand), binary (two operands) and ternary (three operands).
User-defined types can overload an operator by defining it as a static member function using the keyword, ‘operator’ with public level of accessibility. Some operators can only be overloaded with few restrictions.
For example, the operator ‘==’ can be used to compare two objects of immutable value type for which the operator is overloaded in the user-defined type of the object for comparing the value equality instead of reference equality.
The various types of C# operators that are used for specific functionalities include: