**Operators in c# programming** -:The operators of an expression indicate which operations to apply to the operands. Examples of operators include +, -, *, /, and new. Examples of operands include literals, fields, local variables, and expressions.

In the c# programming language has three types operators

**Unary operators.**-:The unary operators take one operand and use either prefix notation (such as –x) or post fix notation (such as x++).**Binary operators.-:**The binary operators take two operands and all use infix notation (such as x + y). Such as a Arithmetic operator,logical operator,Relation Operator,Bitwise operator etc.**Ternary operator.-:**Only one ternary operator, ?:, exists; it takes three operands and uses infix notation (c? x: y).

**Certain operators can be overloaded. Operator overloading permits user-defined operator**

**Operator Precedence and Associativity-: **When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the binary + operator.

The precedence of an operator is established by the definition of its associated grammar production. For example, an additive-expression consists of a sequence of multiplicative-expressions separated by + or – operators, thus giving the + and – operators lower precedence than the *, /, and % operators.

**Operator Overloading-: **All unary and binary operators have predefined implementations that are automatically available in any expression. In addition to the predefined implementations, user-defined implementations can be introduced by including operator declarations in classes and structs.

User-defined operator implementations always take precedence over predefined operator implementations: Only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered.

The overloading unary operators are:

**+ – ! ~ ++ — true false**

Although true and false are not used explicitly in expressions (and therefore are not included in the precedence , they are considered operators because they are invoked in several expression

The overloading binary operators are:

**+ – * / % & | ^ << >> == != > < >= <=**

Only the operators listed above can be overloaded. In particular, it is not possible to overload member access, method invocation, or the =, &&, ||, ??, ?:, =>, checked, unchecked, new, type of, default, as, and is operators.

When a binary operator is overloaded, the corresponding assignment operator, It is also implicitly overloaded. For example, an overload of operato**r * i**s also an overload of operator ***=.** This is described further in §7.17.2. Note that the assignment operator itself (=) cannot be overloaded. An assignment always performs a simple bitwise copy of a value into a variable.

Element access, such as a[x], is not considered an over loadable operator.

In expressions, operators are referenced using operator notation, and in declarations, operators are referenced using functional notation. The following table shows the relationship between operator and functional notations for unary and binary operators. In the first entry, op denotes any over loadable unary prefix operator. In the second entry, op denotes the unary postfix ++ and — operators. In the third entry, op denotes any over loadable binary operator.

**Unary Operator Overload Resolution**-: An operation of the form op x or x op, where op is an overloadable unary operator, and x is an expression of type X.

User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration.

**Unary Operator Overload Resolution-:** An operation of the form op x or x op, where op is an overloadable unary operator, and x is an expression of type X, is processed as follows:

- The set of candidate user-defined operators provided by X for the operation operator op(x) is determined using the rules.
- If the set of candidate user-defined operators is not empty, then this becomes the set of candidate operators for the operation. Otherwise, the predefined unary operator op implementations, including their lifted forms, become the set of candidate operators for the operation.

**Binary Operator Overload Resolution-:** An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X, and y is an expression of type Y, is processed as follows:

- The set of candidate user-defined operators provided by X and Y for the operation operator op(x, y) is determined. The set consists of the union of the candidate operators provided by X and the candidate operators provided by Y, each determined using the rules.
- If the set of candidate user-defined operators is not empty, then this becomes the set of candidate operators for the operation. Otherwise, the predefined binary operator op implementations, including their lifted forms, become the set of candidate operators for the operation. The predefined implementations of a given operator are specified in the description of the operator

**Candidate User-Defined Operators-: **Given a type T and an operation operator op(A), where op is an overloadable operator and A is an argument list, the set of candidate user-defined operators provided by T for operator op(A) is determined as follows:

- Determine the type T0. If T is a nullable type, T0 is its underlying type; otherwise, T0 is equal to T.
- For all operator op declarations in T0 and all lifted forms of such operators if at least one operator is applicable with respect to the argument list A, then the set of candidate operators consists of all such applicable operators in T0.
- Otherwise, if T0 is object, the set of candidate operators is empty.
- Otherwise, the set of candidate operators provided by T0 is the set of candidate operators provided by the direct base class of T0, or the effective base class of T0 if T0 is a type parameter.

**Numeric Promotions-: **Numeric promotion consists of automatically performing certain implicit conversions of the operands of the predefined unary and binary numeric operators. Numeric promotion is not a distinct mechanism, but rather an effect of applying overload resolution to the predefined operators. Numeric promotion specifically does not affect evaluation of user-defined operators, although user-defined operators can be implemented to exhibit similar effects.

As an example of numeric promotion, consider the predefined **implementations of the binary * operator:**

int operator *(int x, int y);

uint operator *(uint x, uint y);

long operator *(long x, long y);

ulong operator *(ulong x, ulong y);

float operator *(float x, float y);

double operator *(double x, double y);

decimal operator *(decimal x, decimal y);

When overload resolution rules are applied to this set of operators, the effect is to select the first of the operators for which implicit conversions exist from the operand types.

For example, for the operation b * s, where b is a byte and s is a short, overload resolution selects operator *(int, int) as the best operator. Thus the effect is that b and s are converted to int, and the type of the result is int. Likewise, for the operation i * d, where i is an int and d is a double, overload resolution selects operator *(double, double) as the best operator.

**Unary Numeric Promotions-: **Unary numeric promotion occurs for the operands of the predefined +, –, and ~ unary operators. Unary numeric promotion simply consists of converting operands of type bytes, byte, short, ushort, or char to type int. Additionally, for the unary – operator, unary numeric promotion converts operands of type int to type long.

** Binary Numeric Promotions-: **Binary numeric promotion occurs for the operands of the predefined +, –, *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:

- If either operand is of type decimal, the other operand is converted to type decimal, or a binding-time error occurs if the other operand is of type float or double.
- Otherwise, if either operand is of type double, the other operand is converted to type double.
- Otherwise, if either operand is of type float, the other operand is converted to type float.
- Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a binding-time error occurs if the other operand is of type sbyte, short, int, or long.
- Otherwise, if either operand is of type long, the other operand is converted to type long
- Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.
- Otherwise, if either operand is of type uint, the other operand is converted to type uint.
- Otherwise, both operands are converted to type int.

Note that the first rule disallows any operations that mix the decimal type with the double and float types. The rule follows from the fact that there are no implicit conversions between the decimal type and the double and float types.

It is not possible for an operand to be of type ulong when the other operand is of a signed integral type. The reason is that no integral type exists that can represent the full range of ulong as well as the signed integral types.

In both of the above cases, a cast expression can be used to explicitly convert one operand to a type that is compatible with the other operand.

**In the example**

decimal AddPercent(decimal x, double percent)

{

return x * (1.0 + percent / 100.0);

}

a binding-time error occurs because a decimal cannot be multiplied by a double. The error is resolved by explicitly converting the second operand to decimal, as follows:

decimal AddPercent(decimal x, double percent)

{

return x * (decimal)(1.0 + percent / 100.0);

}

**Lifted operators-:It** permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. Lifted operators are constructed from predefined and user-defined operators that meet certain requirements, as described in the following list:

**For the unary operators ****+ ++ – — ! ~**

**For the binary operators ****+ – * / % & | ^ << >>**

**For the relational operators ****< > <= >=**