Documentos de Académico
Documentos de Profesional
Documentos de Cultura
com/
Operators
In this part of the C# tutorial, we will talk about operators. Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator. The following table shows a set of operators used in the C# language.
Category
Symbol
Sign operators
+ -
Arithmetic
+ - * / %
String concatenation
Increment, decrement
++ --
Shift
<< >>
Relational
Assignment
Member access
Indexing
[]
Cast
()
Ternary
?:
+ -
Object creation
new
Type information
as is sizeof typeof
checked unchecked
* -> [] &
Lambda
=>
An operator usually has one or two operands. Those operators that work with only one operand are called unary operators. Those who work with two operands are called binary operators. There is also one ternary operator (?:), which works with three operands. Certain operators may be used in different contexts. For example the + operator. From the above table we can see, that it is used in different cases. It adds numbers, concatenates strings or delegates; indicates the sign of a number. We say, that the operator is overloaded.
Sign operators
There are two sign operators. + and -. They are used to indicate or change the sign of a value.
using System; public class CSharpApp { static void Main() { Console.WriteLine(2); Console.WriteLine(+2); Console.WriteLine(-2); } }
+ and - signs indicate the sign of a value. The plus sign can be used to indicate that we have a positive number. It can be omitted and it is mostly done so.
using System; public class CSharpApp { static void Main() { int a = 1; Console.WriteLine(-a); // prints -1 Console.WriteLine(-(-a)); // prints 1 } }
The previous expression does not make sense in mathematics. But it is legal in programming. The expression adds 1 to the x variable. The right side is equal to 2 and 2 is assigned to x.
3 = x;
This code example results in syntax error. We cannot assign a value to a literal.
Concatenating strings
In C# the + operator is also used to concatenate strings.
using System; public class CSharpApp { static void Main() { Console.WriteLine("Return " + "of " + "the king."); } }
We initiate the x variable to 6. Then we increment the x two times. Now the variable equals to 8.
x--;
Arithmetic operators
The following is a table of arithmetic operators in C#.
Symbol
Name
Addition
Subtraction
Multiplication
Division
Remainder
In the preceding example, we use addition, subtraction, multiplication, division and remainder operations. This is all familiar from the mathematics.
int rem = c % a;
The % operator is called the remainder or the modulo operator. It finds the remainder of division of one number by another. For example, 9 % 4, 9 modulo 4 is 1, because 4 goes into 9 twice with a remainder of 1.
$ ./arithmetic.exe 33 2 110 4 2
Next we will show the distinction between integer and floating point division.
using System; public class CSharpApp { static void Main() { int c = 5 / 2; Console.WriteLine(c); double d = 5 / 2.0; Console.WriteLine(d); } }
In this code, we have done integer division. The returned value of the division operation is an integer. When we divide two integers the result is an integer.
double d = 5 / 2.0; Console.WriteLine(d);
If one of the values is a double or a float, we perform a floating point division. In our case, the second operand is a double so the result is a double.
$ ./division.exe 2 2.5
Boolean operators
In C#, we have the following logical operators. Boolean operators are also called logical.
Symbol
Name
&&
logical and
||
logical or
negation
static void Main() { int x = 3; int y = 8; Console.WriteLine(x == y); Console.WriteLine(y > x); if (y > x) { Console.WriteLine("y is greater than x"); } } }
Many expressions result in a boolean value. Boolean values are used in conditional statements.
Console.WriteLine(x == y); Console.WriteLine(y > x);
Relational operators always result in a boolean value. These two lines print false and true.
if (y > x) { Console.WriteLine("y is greater than x"); }
The body of the if statement is executed only if the condition inside the parentheses is met. The y > x returns true, so the message "y is greater than x" is printed to the terminal.
using System; public class CSharpApp { static void Main() { bool a = true && true; bool b = true && false;
bool c = false && true; bool d = false && false; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); } }
Example shows the logical and operator. It evaluates to true only if both operands are true.
$ ./andoperator.exe True False False False
If one of the sides of the operator is true, the outcome of the operation is true.
$ ./orop.exe True True True False
The ||, and && operators are short circuit evaluated. Short circuit evaluation means that the second argument is only evaluated if the first
argument does not suffice to determine the value of the expression: when the first argument of the logical and evaluates to false, the overall value must be false; and when the first argument of logical or evaluates to true, the overall value must be true. (wikipedia) Short circuit evaluation is used mainly to improve performance. An example may clarify this a bit more.
using System; public class CSharpApp { static void Main() { Console.WriteLine("Short circuit"); if (One() && Two()) { Console.WriteLine("Pass"); } Console.WriteLine("#############"); if (Two() || One()) { Console.WriteLine("Pass"); } } public static bool One() { Console.WriteLine("Inside one"); return false; } public static bool Two() { Console.WriteLine("Inside two"); return true; } }
We have two methods in the example. They are used as operands in boolean expressions. We will see, if they are called or not.
if (One() && Two()) { Console.WriteLine("Pass"); }
The One() method returns false. The short circuit && does not evaluate the second method. It is not necessary. Once an operand is false, the result of the logical conclusion is always false. Only "Inside one" is only printed to the console.
Console.WriteLine("#############"); if (Two() || One()) { Console.WriteLine("Pass"); }
In the second case, we use the || operator and use the Two() method as the first operand. In this case, "Inside two" and "Pass" strings are printed to the terminal. It is again not necessary to evaluate the second operand, since once the first operand evaluates to true, the logical or is always true.
$ ./shortcircuit.exe Short circuit Inside one ############# Inside two Pass
Relational Operators
Relational operators are used to compare values. These operators always result in boolean value. Relational operators are also called comparison operators.
Symbol
Meaning
<
less than
<=
>
greater than
>=
==
equal to
!=
not equal to
< 4); ' prints True == 4); ' prints False >= 3); ' prints True != 3); ' prints True
In C# we use the == to compare numbers. Some languages like Ada, Visual Basic, or Pascal use = for comparing numbers.
Bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal or hexadecimal symbols are only notations of the same number. Bitwise operators work with bits of a binary number. Bitwise operators are seldom used in higher level languages like C#.
Symbol
Meaning
bitwise negation
bitwise exclusive or
&
bitwise and
bitwise or
The operator reverts all bits of a number 7. One of the bits also determines, whether the number is negative or not. If we negate all the bits one more time, we get number 7 again. The bitwise and operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 only if both corresponding bits in the operands are 1.
00110 & = 00011 00010
The bitwise or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if either of the corresponding bits in the operands is 1.
The bitwise exclusive or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if one or the other (but not both) of the corresponding bits in the operands is 1.
00110 ^ = 00011 00101
Console.WriteLine(a); // prints 7 } }
The += compound operator is one of these shorthand operators. They are less readable than the full expressions but experienced programmers often use them. Other compound operators are:
-= *= /= %= &= |= <<= >>=
Type information
Now we will concern ourselves with operators that work with types. The sizeof operator is used to obtain the size in bytes for a value type. The
typeof
using System; public class CSharpApp { static void Main() { Console.WriteLine(sizeof(int)); Console.WriteLine(sizeof(float)); Console.WriteLine(sizeof(Int32)); Console.WriteLine(typeof(int)); Console.WriteLine(typeof(float)); } }
4 4 System.Int32 System.Single
Output. We can see, that the int type is an alias for System.Int32 and the
float
We have a Base and a Derived class. The Derived class inherits from the Base class.
Console.WriteLine(_base is Base); Console.WriteLine(_base is Object);
Base equals Base and so the first line print True. A Base is also compatible with Object type. This is because each class inherits from the mother of all classes, the Object class.
Console.WriteLine(derived is Base); Console.WriteLine(_base is Derived);
The derived object is compatible with the Base class, because it explicitly inherits from the Base class. On the other hand, the _base object has nothing to do with the Derived class.
$ ./isoperator.exe True True True False
Output of example.
The as operator is used to perform conversions between compatible reference types. When the conversion is not possible, the operator returns null. Unlike the cast operation which raises an exception.
using System; class Base {} class Derived : Base {} public class CSharpApp { static void Main() { object[] objects = new object[6]; objects[0] = new Base(); objects[1] = new Derived(); objects[2] = "ZetCode"; objects[3] = 12; objects[4] = 1.4; objects[5] = null;
for (int i=0; i<objects.Length; ++i) { string s = objects[i] as string; Console.Write ("{0}:", i); if (s != null) Console.WriteLine (s); else Console.WriteLine ("not a string"); } } }
We try to cast various types to the string type. But only once the casting is valid.
$ ./asoperator.exe 0:not a string 1:not a string 2:ZetCode 3:not a string 4:not a string 5:not a string
Output.
Operator precedence
The operator precedence tells us which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions. What is the outcome of the following expression? 28 or 40?
3 + 5 * 5
Like in mathematics, the multiplication operator has a higher precedence than addition operator. So the outcome is 28.
(3 + 5) * 5
To change the order of evaluation, we can use parentheses. Expressions inside parentheses are always evaluated first. The following table shows common C# operators ordered by precedence (highest precedence first):
Operator(s)
Category
Associativity
x.y f(x) a[x] x++ x-- new typeof default Primary checked unchecked Left
Unary
Left
Multiplicative
* / %
Left
Additive
+ -
Left
Shift
<< >>
Left
Equality
== !=
Right
Logical AND
&
Left
Logical XOR
Left
Logical OR
Left
Conditional AND
&&
Left
Conditional OR
||
Left
Null Coalescing
??
Left
Ternary
?:
Right
Assignment
Right
Operators on the same row of the table have the same precedence.
using System; public class CSharpApp { static void Main() { Console.WriteLine(3 + 5 * 5); Console.WriteLine((3 + 5) * 5); Console.WriteLine(! true | true); Console.WriteLine(! (true | true)); } }
In this code example, we show some common expressions. The outcome of each expression is dependent on the precedence level.
Console.WriteLine(3 + 5 * 5);
This line prints 28. The multiplication operator has a higher precedence than addition. First the product of 5*5 is calculated. Then 3 is added.
Console.WriteLine(! true | true);
In this case, the negation operator has a higher precedence. First, the first true value is negated to false, than the | operator combines false and true, which gives true in the end.
$ ./precedence.exe 28 40 True False
Associativity
Sometimes the precedence is not satisfactory to determine the outcome of an expression. There is another rule called associativity. The associativity of operators determines the order of evaluation of operators with the same precedence level.
9 / 3 * 3
What is the outcome of this expression? 9 or 1? The multiplication, deletion and the modulo operator are left to right associated. So the expression is evaluated this way: (9 / 3) * 3 and the result is 9. Arithmetic, boolean, relational and bitwise operators are all left to right associated. On the other hand, the assignment operator is right associated.
int a, b, c, d; a = b = c = d = 0; Console.WriteLine("{0} {1} {2} {3}", a, b, c, d); // prints 0 0 0 0
If the association was left to right, the previous expression would not be possible. The compound assignment operators are right to left associated.
int j = 0; j *= 3 + 1; Console.WriteLine(j);
You might expect the result to be 1. But the actual result is 0. Because of the associativity. The expression on the right is evaluated first and than the compound assignment operator is applied.
We want to assign a value to z variable. But it must not be null. This is our requirement. We can easily use the null-coalescing operator for that. In case both x, y variables are null, we assign -1 to z.
$ ./nullcoalescing.exe -1
Output of program.
If cond-exp is true, exp1 is evaluated and the result is returned. If the condexp is false, exp2 is evaluated and its result is returned.
using System; public class CSharpApp { static void Main() { int age = 31; bool adult = age >= 18 ? true : false; Console.WriteLine("Adult: {0}", adult); }
In most countries the adulthood is based on your age. You are adult if you are older than a certain age. This is a situation for a ternary operator.
bool adult = age >= 18 ? true : false;
First the expression on the right side of the assignment operator is evaluated. The first phase of the ternary operator is the condition expression evaluation. So if the age is greater or equal to 18, the value following the ? character is returned. If not, the value following the : character is returned. The returned value is then assigned to the adult variable.
$ ./ternary.exe Adult: True
using System; using System.Collections.Generic; public class CSharpApp { static void Main() { List<int> list = new List<int>() { 3, 2, 1, 8, 6, 4, 7, 9, 5 }; List<int> sublist = list.FindAll(val => val > 3); foreach (int i in sublist) { Console.WriteLine(i); } } }
We have a list of integer numbers. We print all numbers that are greater than 3.
List<int> list = new List<int>() { 3, 2, 1, 8, 6, 4, 7, 9, 5 };
Here we use the lambda operator. The FindAll() method takes a predicate as a parameter. A predicate is a special kind of a delegate, that returns a boolean value. The predicate is applied for all items of the list. The val is an input parameter, specified without a type. We could explicitly specify the type, but it is not necessary. The compiler will expect an int type. The val is a current input value from the list. It is compared if it is greater than 3 and a boolean true or false is returned. Finally, the FindAll() will return all values that met the condition. They are assigned to the sublist collection.
foreach (int i in sublist) { Console.WriteLine(i); }
$ ./lambda.exe 8 6 4 7 9 5
This is the same example. We use a anonymous delegate instead of a lambda expression.
public class CSharpApp { static void Main() { int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 }; Console.Write("Prime numbers: "); foreach (int num in nums) { if (num == 1 || num == 2 || num == 3) { Console.Write(num + " "); continue; } int i = (int) Math.Sqrt(num); bool isPrime = true; while (i > 1) { if (num % i == 0) { isPrime = false; } i--; } if (isPrime) { Console.Write(num + " "); } } Console.Write('\n'); } }
In the above example, we deal with many various operators. A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. We pick up a number and divide it by numbers, from 1 up to the picked up number. Actually, we don't have to try all smaller numbers, we can divide by numbers up to the square root of the chosen number. The formula will work. We use the remainder division operator.
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
We skip the calculations for the 1, 2, 3 numbers. They are primes. Note the usage of the equality and conditional or operators. The == has a higher precedence than the || operator. So we don't need to use parentheses.
int i = (int) Math.Sqrt(num);
We are OK if we only try numbers smaller than the square root of a number in question.
while (i > 1) { ... i--; }
This is a while loop. The i is the calculated square root of the number. We use the decrement operator to decrease the i by one each loop cycle. When the i is smaller than 1, we terminate the loop. For example, we have number 9. The square root of 9 is 3. We will divide the 9 number by 3 and 2. This is sufficient for our calculation.
if (num % i == 0) { isPrime = false; }
This is the core of the algorithm. If the remainder division operator returns 0 for any of the i values, than the number in question is not a prime.
HTML Server Control HtmlAnchor Description Controls an <a> HTML element
Controls a <button> HTML element Controls a <form> HTML element Controls other HTML element not specified by a specific HTML server control, like <body>, <div>, <span>, etc. Controls an <image> HTML element Controls <input type="button">, <input type="submit">, and <input type="reset"> HTML elements Controls an <input type="checkbox"> HTML element Controls an <input type="file"> HTML element Controls an <input type="hidden"> HTML element Controls an <input type="image"> HTML element Controls an <input type="radio"> HTML element Controls <input type="text"> and <input type="password"> HTML elements Controls a <select> HTML element Controls a <table> HTML element Controls <td>and <th> HTML elements Controls a <tr> HTML element Controls a <textarea> HTML element
HtmlImage HtmlInputButton