C# – Syntax (Operands & Operators)

Again let’s jump into c# syntax basically and let me show you what you need to know in the beginning.

Operands -> variable names, object/server control names, literals – “Nouns”(you name these)

Operators – > “Verbs”… they act on operands

A detailed explanation could be found in the following taken from microsoft.com

In C#, an operator is a program element that is applied to one or more operands in an expression or statement. Operators that take one operand, such as the increment operator (++) or new, are referred to as unary operators. Operators that take two operands, such as arithmetic operators (+,-,*,/), are referred to as binary operators. One operator, the conditional operator (?:), takes three operands and is the sole ternary operator in C#.

The following C# statement contains a single unary operator and a single operand. The increment operator, ++, modifies the value of the operand y.


The following C# statement contains two binary operators, each with two operands. The assignment operator, =, has the integer variable y and the expression 2 + 3 as operands. The expression 2 + 3 itself consists of the addition operator and two operands, 2 and 3.

y = 2 + 3;

Operators, Evaluation, and Operator Precedence

An operand can be a valid expression that is composed of any length of code, and it can comprise any number of sub expressions. In an expression that contains multiple operators, the order in which the operators are applied is determined by operator precedenceassociativity, and parentheses.

Each operator has a defined precedence. In an expression that contains multiple operators that have different precedence levels, the precedence of the operators determines the order in which the operators are evaluated. For example, the following statement assigns 3 to n1.

n1 = 11 - 2 * 4;

The multiplication is executed first because multiplication takes precedence over subtraction.

The following table separates the operators into categories based on the type of operation they perform. The categories are listed in order of precedence.

Primary Operators

Expression Description


Member access

Conditional member access

f(x) Method and delegate invocation


Array and indexer access

Conditional array and indexer access

x++ Post-increment
x Post-decrement
new T(…) Object and delegate creation
new T(…){…} Object creation with initializer. See Object and Collection Initializers.
new {…} Anonymous object initializer. See Anonymous Types.
new T[…] Array creation. See Arrays.
typeof(T) Obtain System.Type object for T
checked(x) Evaluate expression in checked context
unchecked(x) Evaluate expression in unchecked context
default (T) Obtain default value of type T
delegate {} Anonymous function (anonymous method)

Unary Operators

Expression Description
+x Identity
x Negation
!x Logical negation
~x Bitwise negation
++x Pre-increment
x Pre-decrement
(T)x Explicitly convert x to type T

Multiplicative Operators

Expression Description
* Multiplication
/ Division
% Remainder

Additive Operators

Expression Description
+ y Addition, string concatenation, delegate combination
 y Subtraction, delegate removal

Shift Operators

Expression Description
<< y Shift left
>> y Shift right

Relational and Type Operators

Expression Description
< y Less than
> y Greater than
<= y Less than or equal
>= y Greater than or equal
is T Return true if x is a T, false otherwise
as T Return x typed as T, or null if x is not a T

Equality Operators

Expression Description
== y Equal
!= y Not equal

Logical, Conditional, and Null Operators

Category Expression Description
Logical AND & y Integer bitwise AND, Boolean logical AND
Logical XOR ^ y Integer bitwise XOR, boolean logical XOR
Logical OR | y Integer bitwise OR, boolean logical OR
Conditional AND && y Evaluates y only if x is true
Conditional OR || y Evaluates y only if x is false
Null coalescing ?? y Evaluates to y if x is null, to x otherwise
Conditional ? y : z Evaluates to y if x is true, z if x is false

Assignment and Anonymous Operators

Expression Description
= Assignment
x op= y Compound assignment. Supports these operators: +=-=*=/=%=&=|=!=<<=>>=
(T x) => y Anonymous function (lambda expression)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: