Learn C# Programming Language
Operators in C# programming
Definition of Expressions and Operators
Expressions are a central component of practically every Visual C# application, because expressions are the fundamental constructs that you use to evaluate and manipulate data. Expressions are collections of operands and operators, which you can define as follows:
- Operands are values, for example, numbers and strings. They can be constant (literal) values, variables, properties, or return values from method calls.
- Operators define operations to perform on operands(addition or multiplication). Operators exist for all of the basic mathematical operations, as well as for more advanced operations such as logical comparison or the manipulation of the bits of data that constitute a value.
All expressions are evaluated to a single value when your application runs. The type of value that an expression produces depends on the types of the operands that you use and the operators that you use.
There is no limit to the length of expressions in Visual C# applications, although in practice, you are limited by the memory of your computer and your patience when typing.
However, it is usually advisable to use shorter expressions and assemble the results of expression-processing piecemeal. This makes it easier for you to see what your code is doing, as well as making it easier to debug your code.
Operators in Visual C#
Operators combine operands together into expressions. Visual C# provides a wide range of operators that you can use to perform most fundamental mathematical and logical operations. Operators fall into the following three categories:
- Unary. This type of operator operates on a single operand. For example, you can use the - operator as a unary operator. To do this, you place it immediately before a numeric operand, and it converts the value of the operand to its current value multiplied by –1.
- Binary. This type of operand operates on two values. This is the most common type of operator, for example, *, which multiplies the value of two operands.
- Ternary. There is only one ternary operator in Visual C#. This is the ? : operator that is used in conditional expressions.
The following table shows the Arithmetic Operators which you can use to perform arithmetic operations in Visual C#. The second table shows the precendence of Arithmetic Operators.
C# operation | Arithmetic operator | Algebraic expression | C# expression |
Addition | + | f + 7 | f + 7 |
Subtraction | – | p – c | p - c |
Multiplication | * | b ⋅ m | b * m |
Division | / | x / y or x ÷ y | x / y |
Remainder | % | r mod s | r % s |
Operators | Operations | Order of evaluation (associativity) |
Evaluated first | ||
* | Multiplication | If there are several operators of this type, they’re evaluated from left to right. |
/ | Division | |
% | Remainder | |
Evaluated next | ||
+ | Addition | If there are several operators of this type, they’re evaluated from left to right. |
- | Subtraction |
Equality and Relational Operators
A condition is an expression that can be either true or false. Conditions in if statements can be formed by using the equality operators (== and !=) and relational operators (>, <, >= and >=) .
The two equality operators (== and !=) each have the same level of precedence, the relational operators (>, <, >= and >=) each have the same level of precedence, and the equality operators have lower precedence than the relational operators. They all associate from left to right.
Standard algebraic equality and relational operators | C# equality or relational operator | Sample C# condition | Meaning of C# condition |
Relational operators | |||
> | > | x > y | x is greater than y |
< | < | x < y | x is less than y |
≥ | <= | x <= y | x is greater than or equal to y |
≤ | <= | x <= y | x is less than or equal to y |
Equality operators | |||
= | == | x == y | x is equal to y |
≠ | != | x != y | x is not equal to y |
Other Visual C# Operators
The following table shows the other operators that you can use in Visual C#, grouped by type.
Type | Operators |
Increment, decrement | ++, - - |
String concatenation | + |
Logical/bitwise operations | &, |, ^, !, ~, &&, || |
Indexing (counting starts from element 0) | [ ] |
Casting | ( ), as |
Assignment | =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, ?? |
Bit shift | <<, >> |
Type information | sizeof, typeof |
Delegate concatenation and removal | +, - |
Overflow exception control | checked, unchecked |
Indirection and Address (unsafe code only) | *, ->, [ ], & |
Conditional (ternary operator) | ?: |
Ads Right