This section is mainly for reference, you will refer to this quite regularly in the beginning.
1. What is an expression?
 It is a sequence of operators and operands.
 It specifies computation of a value, or
 It designates a variable or constant.
2. Classification of Expressions
An expression is classified as one of the following:
 A value.
 A variable.
 A namespace.
 A type.
 A method group.
 A property access.
 An event access.
 An indexer access.
 Nothing.
The final result of an expression cannot be one of the following:
 A namespace.
 A type.
 A method group.
 An event access.
These categories are intermediate constructs. They are only permitted in certain contexts.
3. Values of Expressions.
If the expression denotes a property access, an indexer access, or a variable, the value of the property, indexer, or variable is implicitly substituted:
 The value of a variable is simply the value currently stored in the storage location identified by the variable.
 The value of a property access expression is obtained by invoking the getaccessor of the property.
 The value of an indexer access expression is obtained by invoking the getaccessor of the indexer.
4. Operators
Expressions are constructed from operands and operators. Operators of an expression indicate which operations to apply to the operands.
We can find three types of operators:
 Unary Operators: The unary operators take one operand. They use either prefix notation (–i) or postfix notation (i++).
 Binary Operators: The binary operators take two operands. They all use infix notation (i + j).
 Ternary Operator: There exists only one ternary operator, ?:. The ternary operator takes three operands and uses infix notation (z? x: y).
Operands in an expression are evaluated from left to right. Certain operators can be overloaded. This permits userdefined operator implementations to be specified for operations where one or both of the operands are of a userdefined class or struct type.
4.1. Operator Precedence and Association
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 a + b * c is evaluated as a + (b * c) because the * operator has higher precedence than the + operator. The precedence of an operator is established by the definition of its associated grammar production.
When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed:
 All binary operators are leftassociative , except for the assignment operators, meaning that operations are performed from left to right.
 Assignment operators and conditional operator (?:) are rightassociative , which means that operations are performed from right to left.
 Precedence and association can be controlled using parentheses.
4.2. Operator Overloading
Userdefined implementations can be introduced by including operator declarations in classes and structs.
Userdefined operator implementations always take precedence over predefined operator implementations – only when no applicable userdefined operator implementations exist will the predefined operator implementations be considered.
The overloadable unary operators are:
The overloadable binary operators are:

– * / % & 
^ << >> == != > < >= <= 
When a binary operator is overloaded, the corresponding assignment operator (if any) is also implicitly overloaded. For example, an overload of operator + is also an overload of operator +=. The assignment operator (=) cannot be overloaded. An assignment performs a bitwise copy of a value into a variable. Element access, such as Ar[x], is not an overloadable operator.
Userdefined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration. Userdefined operator declarations cannot modify the syntax, precedence, or associativity of an operator. For example, the / operator is always a binary operator, always has the precedence level specified in, and is always leftassociative.
5. Function Members
Function members are members that contain executable statements. They are always members of types and cannot be members of namespaces.
C# defines the following categories of function members:
 Methods
 Properties
 Events
 Indexers
 Userdefined operators
 Instance constructors
 Static constructors
 Destructors
Statements contained in function members are executed through function member invocations. The argument list of a function member invocation provides actual values or variable references for the parameters of the function member.
5.1. The new Operator
The new operator is used to create new instances of types. There are three forms of new expressions:
 Object creation expressions are used to create new instances of class types and value types.
 Array creation expressions are used to create new instances of array types.
 Delegate creation expressions are used to create new instances of delegate types.
The new operator implies creation of an instance of a type. Instances of value types require no additional memory beyond the variables in which they reside.
6. Unary Operators
The unary operators are +, , !, ~, *, ++, –, and cast operators.
6.1. The “+” Operator
The predefined plus operators are:
 int operator +(int x);
 uint operator +(uint x);
 long operator +(long x);
 ulong operator +(ulong x);
 float operator +(float x);
 double operator +(double x);
 decimal operator +(decimal x);
For each of these operators, the result is simply the value of the operand.
6.2. The “” Operator
The predefined negation operators are:
 Integer negation:
 int operator –(int x);
 long operator –(long x);
The result is computed by subtracting x from zero.
 Floatingpoint negation:
 float operator –(float x);
 double operator –(double x);
The result is the value of x with its sign inverted.
 Decimal negation:
 decimal operator –(decimal x);
The result is computed by subtracting x from zero. Decimal negation is equivalent to using the unary minus operator of type Decimal.
6.3. Logical negation operator
There is only one predefined logical negation operator:
 bool operator !(bool x); (Not bool x = when x is false)
This operator computes the logical negation of the operand: if the operand is true, the result is false. If the operand is false, the result is true.
6.4. Bitwise complement operator
The bitwise complement operators are:
 int operator ~(int x);
 uint operator ~(uint x);
 long operator ~(long x);
 ulong operator ~(ulong x);
6.5. Prefix increment and decrement operators
 Preincrementexpression: ++ unaryexpression
 Predecrementexpression: — unaryexpression
The value returned by the operator becomes the result of the operation. The ++ and — operators also support postfix notation.
6.6. Cast Expressions
A castexpression is used to explicitly convert an expression to a given type.
Example:
( type ) unaryexpression
1:
2: /*The number 2 by default is an int, here it is recognised as cast as a float */
3: (float)2
7. Arithmetic Operators
The *, /, %, +, and – operators are called the arithmetic operators.
multiplicativeexpression:
unaryexpression
multiplicativeexpression * unaryexpression
multiplicativeexpression / unaryexpression
multiplicativeexpression % unaryexpression
additiveexpression:
multiplicativeexpression
additiveexpression + multiplicativeexpression
additiveexpression – multiplicativeexpression
7.1. Multiplication Operator
Integer multiplication:
 int operator *(int x, int y);
 uint operator *(uint x, uint y);
 long operator *(long x, long y);
 ulong operator *(ulong x, ulong y);
Floatingpoint multiplication:
 float operator *(float x, float y);
 double operator *(double x, double y);
Decimal multiplication:
 decimal operator *(decimal x, decimal y);
7.2. Division Operator
Integer division:
 int operator /(int x, int y);
 uint operator /(uint x, uint y);
 long operator /(long x, long y);
 ulong operator /(ulong x, ulong y);
Floatingpoint division:
 float operator /(float x, float y);
 double operator /(double x, double y);
Decimal division:
 decimal operator /(decimal x, decimal y);
7.3. Remainder Operator
Integer remainder:
 int operator %(int x, int y);
 uint operator %(uint x, uint y);
 long operator %(long x, long y);
 ulong operator %(ulong x, ulong y);
Floatingpoint remainder:
 float operator %(float x, float y);
 double operator %(double x, double y);
Decimal remainder:
 decimal operator %(decimal x, decimal y);
7.4. Addition Operator
Integer addition:
 int operator +(int x, int y);
 uint operator +(uint x, uint y);
 long operator +(long x, long y);
 ulong operator +(ulong x, ulong y);
Floatingpoint addition:
 float operator +(float x, float y);
 double operator +(double x, double y);
Decimal addition:
 decimal operator +(decimal x, decimal y);
Enumeration addition:
 E operator +(E x, U y);
 E operator +(U x, E y);
String concatenation:
 string operator +(string x, string y);
 string operator +(string x, object y);
 string operator +(object x, string y);
7.5. Subtraction Operator
Integer subtraction:
 int operator –(int x, int y);
 uint operator –(uint x, uint y);
 long operator –(long x, long y);
 ulong operator –(ulong x, ulong y);
Floatingpoint subtraction:
 float operator –(float x, float y);
 double operator –(double x, double y);
Decimal subtraction:
 decimal operator –(decimal x, decimal y);
Enumeration subtraction:
 U operator –(E x, E y);
 E operator –(E x, U y);
8. Shift Operators
The << and >> operators are used to perform bit shifting operations.
8.1 shiftexpression
 additiveexpression
 shiftexpression << additiveexpression
 shiftexpression >> additiveexpression
When declaring an overloaded shift operator, the type of the first operand must always be the class or struct containing the operator declaration, and the type of the second operand must always be int.
The predefined shift operators are listed below.
8.2 Shift Left
 int operator <<(int x, int count);
 uint operator <<(uint x, int count);
 long operator <<(long x, int count);
 ulong operator <<(ulong x, int count);
The << operator shifts x left by a number of bits computed as described below. The highorder bits outside the range of the result type of x are discarded, the remaining bits are shifted left, and the loworder empty bit positions are set to zero.
8.3 Shift Right
 int operator >>(int x, int count);
 uint operator >>(uint x, int count);
 long operator >>(long x, int count);
 ulong operator >>(ulong x, int count);
The >> operator shifts x right by a number of bits computed as described below.
9. Relational & Type Testing Operators
The ==, !=, <, >, <=, >=, is and as operators are called the relational and type testing operators.
9.1. RelationalExpression
shiftexpression
 relationalexpression < shiftexpression
 relationalexpression > shiftexpression
 relationalexpression <= shiftexpression
 relationalexpression >= shiftexpression
 relationalexpression is type
 relationalexpression as type
9.2. EqualityExpression
relationalexpression
 equalityexpression == relationalexpression
 equalityexpression != relationalexpression
All comparison operators return a result of type bool, as described in the following table:
Operation Result
 x == y true if x is equal to y, false otherwise
 x != y true if x is not equal to y, false otherwise
 x < y true if x is less than y, false otherwise
 x > y true if x is greater than y, false otherwise
 x <= y true if x is less than or equal to y, false otherwise
 x >= y true if x is greater than or equal to y, false otherwise
9.3. The as Operator
This operator is used to explicitly convert a value to a given reference type using a reference conversion or a boxing conversion. The as operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is null.
10. Logical Operators
The &, ^, and 
operators are called the logical operators. 
equalityexpression
andexpression & equalityexpression
andexpression
exclusiveorexpression ^ andexpression
exclusiveorexpression
inclusiveorexpression 
exclusiveorexpression 
10.1. Conditional Operator
The ?: operator is called the conditional operator. It is sometimes called the ternary operator. A conditional expression of the form b? x: y first evaluates the condition b. Then, if b is true, x is evaluated and becomes the result of the operation. Otherwise, y is evaluated and becomes the result of the operation. A conditional expression never evaluates both x and y. The conditional operator is rightassociative, meaning that operations are grouped from right to left. The first operand of the ?: operator must be an expression of a type that can be implicitly converted to bool, or an expression of a type that implements operator true. If neither requirement is satisfied, a compiletime error occurs. The second and third operands of the ?: operator control the type of the conditional expression.
10.2. Assignment Operators
Assignment operators assign a new value to a variable, a property, or an indexer element. The left operand of an assignment must be an expression classified as a variable, a property access, or an indexer access. The = operator is called the simple assignment operator. It assigns the value of the right operand to the variable, property, or indexer element given by the left operand. The operators formed by prefixing a binary operator with an = character are called the compound assignment operators. These operators perform the indicated operation on the two operands, and then assign the resulting value to the variable, property, or indexer element given by the left operand. The assignment operators are rightassociative, meaning that operations are grouped from right to left. For example, an expression of the form a = b = c is evaluated as a = (b = c).
10.3. Simple Assignment
The = operator is called the simple assignment operator. In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand. The operation assigns the value of the right operand to the variable, property, or indexer element given by the left operand. The result of a simple assignment expression is the value assigned to the left operand. The result has the same type as the left operand and is always classified as a value. If the left operand is a property or indexer access, the property or indexer must have a set accessor. If this is not the case, a compiletime error occurs.
10.4. Boolean Expressions
A booleanexpression is an expression that yields a result of type bool. The controlling conditional expression of an ifstatement, whilestatement, dostatement, or forstatement is a booleanexpression. The controlling conditional expression of the ?: operator follows the same rules as a booleanexpression, but for reasons of operator precedence is classified as a conditionalorexpression. A booleanexpression is required to be of a type that can be implicitly converted to bool or of a type that implements operator true. If neither requirement is satisfied, a compiletime error occurs. When a boolean expression is of a type that cannot be implicitly converted to bool but does implement operator true, then following evaluation of the expression, the operator true implementation provided by that type is invoked to produce a bool value.
Simon (darkside) Jackson
Engineer, industry executive, research enthusiast. Avid learner with diverse interests in coding, game development, Mixed Reality (AR/VR/XR) and reinforcement learning. 25+ years of experience working in multinational corporations and startups.
Comments