C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
So far, we have seen parameters
. Let's see var, expressions and precedence in C#
in this chapter.
The main objective of this article is to learn about var
type, different types of expressions and precedence in C# programming.
is an implicit type which is used instantly to declare and initialize a variable
. It is a C#
can be used instead of any keywords
such as int
Below example explains the usage of var clearly,
static void Main()
var a = "This is a string"; // string
var b = new StringBuilder(); // string builder
var c = (float)Math.PI; // float
var d = 'x'; // char
var i = 1; // int
Console.WriteLine(a); // This is a string
Console.WriteLine(c); // 3.14 as it is the value of PI
Console.WriteLine(d); // x
Console.WriteLine(i); // 1
In the above code, we have used var
to declare an
etc. which performs all their actions.
Note: Please read the comments in the code,
Now, check the result in the console
which looks like,
But this implicitly typed variables are statically typed variables. For example,
int i = 2;
i = 'a'; // gives compile time error because 'i' is declared as int.Note: Please read the comments in the code
is a value. There are two simple kinds of expressions, which are constants
are used to combine
or to transform
Constant expression: Constant expression is an expression which doesn't perform any operation and will be constant. Example,
Combining expressions with operators
Let's take an arithmetic operator and combine two
operands like below,
5 * 12
In the above example, '
' is an operator used to combine two operands
We can also create complex expressions like below,
100 - (5*12)
In the above example, we have '
' is an operand which itself behaves as an expression.
C# operators are derived as
binary (two) and
ternary (three) depending on the no. of operands they work.
Binary operators always use
infix notation in which the operator is placed between two operands.
These are the simplest form of expressions. They include the expressions composed of operators.
In the above expression, we have two primary
expressions. First one (with
operator) checks the member and second expressions calls the function with '
These are the empty valued expressions which has no value.
In the above example, we are just printing something but there is no value declared.
Void Expressions cannot be used as operands for generating complex expressions. Such as,
Console.Write("Void") + 2; // Compile-time error
The above expression gives you the
compile time error
as the void expression
we used has no value.
These type of expressions will perform the action and assigns the result of that expression to a variable by using '
a = a + 4
The above expression is a valid expression (not void) as it has an assigned value. It adds the numeric value '
and stores the result in
Hence, it can be used in another expression such as,
b = 2 * (a = 3)
The above code assigns
Assignment expressions are used to initialize multiple values in the same expression. For example,
a = b = c = 5 // Which means a = 5, b = 5, c = 5
There are some compound assignment operators
a += 4 // This means a = a + 4
These are known as the syntactic shortcuts.
Operator Precedence and Associativity
precedence and associativity determines the order of evaluation of an expression which has multiple operators. Priority of execution is given to the operators with higher precedence.
If we have the same kind of operators in an expression, then associativity helps in determining the order of evaluation.
Let's take an example expression with multiple operators,
15 - 4 * 3
The above expression performs '
' first because of its higher precedence (BODMAS
rule). So it becomes as,
All the binary operators are left associative except the assignment, lambda and null coalescing operators.
Left-associative means they will execute from left to right.
4 * 2 * 2 // 16
This will be executed as,
(4 * 2) * 2 // 16
Now let's take another example like,
16 / 8 / 2
This should be evaluated as,
(16 / 8) / 2 // 1
If we change the order of evaluation,
Note: please see the difference in comments.
Right-associative operators are the assignment operators, null coalescing, lambda and conditional operators.
a = b = c = 5
The above expression assigns the value '
) and then to
(right to left
) and so on.
In this article, we have looked into 'var' and different types of expressions in C# programming. Hope you understand.
Thanks for reading.