C# Operators
Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two
values:
Example
int
x
= 100 + 50
;
Although the + operator is often used to add together two values, like
in the example above, it can also
be used to add together a variable and a value, or a variable and another variable:
Example
int
sum1
= 100 + 50
; // 150 (100 + 50)
int
sum2
= sum1 + 250
; // 400 (150 + 250)
int
sum3
= sum2 + sum2
; // 800 (400 + 400)
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations:
Operator
|
Name
|
Description
|
Example
|
+     
|
Addition     
|
Adds together two values     
|
x+y     
|
-    
|
Subtraction     
|
Subtracts one value from another     
|
x-y    
|
*    
|
Multiplication     
|
Multiplies two values     
|
x*y    
|
/    
|
Division     
|
Divides one value by another     
|
x/y    
|
%    
|
Modulus     
|
Returns the division remainder     
|
x%y    
|
++    
|
Increment     
|
Increases the value of a variable by 1     
|
x++    
|
--    
|
Decrement     
|
Decreases the value of a variable by 1     
|
x--    
|
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to
assign the value 10 to a variable
called x:
Example
int
x
= 10
;
The addition assignment operator (+=) adds a value to a
variable:
Example
int
x
= 10
;
x+=5;
A list of all assignment operators:
Operator     |
Example     |
Same as     |
=    
|
x=5    
|
x=5    
|
+=    
|
x+=3    
|
x=x+3    
|
-=    
|
x-=3    
|
x=x-3    
|
*=    
|
x*=3    
|
x=x*3    
|
/=    
|
x/=3    
|
x=x/3    
|
%=    
|
x%=3    
|
x=x%3    
|
&=    
|
x&=3    
|
x=x&3    
|
|=    
|
x|=3    
|
x=x|3    
|
^=    
|
x^=3    
|
x=x^3 |
>>=    
|
x>>=3    
|
x=x>>3    
|
<<=     
|
x<<=3     
|
x=x<<3     
|
Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in programming,
because it helps us to find answers and make decisions.
The return value of a comparison is either True or True. These values are known as Boolean values, and
you will learn more about them in the Booleans and If..Else
chapter.
In the following example, we use the greater than operator (>) to
find out if 5 is greater than
3:
Example
int
x
= 5
;
int
x
= 3
;
Console.WriteLine(
x
>
y
); // returns True because 5 is greater than 3
A list of all comparison operators:
Operator     |
Name     |
Example     |
==    
|
Equal to    
|
x==y    
|
!=    
|
Not equal    
|
x!=y    
|
>    
|
Greater than    
|
x>y    
|
<    
|
Less than    
|
x< y     
|
>=    
|
Greater than or equal to    
|
x>=y    
|
<=     
|
Less than or equal to    
|
x<= y    
|
Logical Operators
As with comparison operators, you can also test for True or False
values with logical operators.
Logical operators are used to determine the logic between variables or values:
Operator     |
Name     |
Description     |
Example     |
&&   
|
Logical and   
|
Returns True if both statements are true   
|
x<5 && x<10    
|
||   
|
Logical or   
|
Returns True if one of the statements is true   
|
x<5 || x<4   
|
!   
|
Logical not   
|
Reverse the result, returns False if the result is true   
|
!(x<5 && x< 10)   
|
You will learn more about comparison and logical operators in the Booleans and If...Else
chapters.
C# Strings
C# Strings
Strings are used for storing text.
A string variable contains a collection of characters surrounded by
double quotes:
Example
Create a variable of type string and
assign it a value:
string greeting="Hello";
A string variable can contain many words, if you want:
Example
string greeting2="Nice to meet you!";
String Length
A string in C# is actually an object, which contain properties and methods that can perform certain
operations on strings. For example, the length of a string can be found with the Length property:
Example
string txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console .WriteLine(
"The length of the txt string is: " +
txt . Length );
Other Methods
There are many string methods available, for example ToUpper() and ToLower(), which returns a copy of the
string converted to uppercase or lowercase:
Example
string txt="Hello World";
Console .WriteLine(
txt .ToUpper()); //
Outputs "HELLO WORLD"
Console .WriteLine(
txt .ToLower()); //
Outputs "hello world"
C# Booleans
C# Booleans
Very often, in programming, you will need a data type that can only have one of two values,
like:
• YES / NO
• ON / OFF
• TRUE / FALSE
For this, C# has a bool data type, which can take the values true or false.
Boolean Values
A boolean type is declared with the bool keyword and can only take the values true or false:
Example
bool isCSharpfun=true;
bool isFishTasty=false;
Console .WriteLine(
isCSharpfun); // Outputs True
Console .WriteLine(
isFishTasty); // Outputs False
However, it is more common to return boolean values from boolean expressions, for conditional testing (see
below).
Boolean Expression
A Boolean expression returns a boolean value: True or False, by comparing values/variables.
This is useful to build logic, and find answers.
For example, you can use a comparison operator, such as the
greater than (
>) operator to find out if an
expression (or a variable) is true:
Example
int x=10;
int y=9;
Console .WriteLine(
x>y); // returns True,
because 10 is higher than 9
Or even easier:
Example
Console .WriteLine(
10>9); // returns True,
because 10 is higher than 9
In the examples below, we use the equal to (==) operator to
evaluate an
expression:
Example
int x=10;
Console .WriteLine(
x==10); // returns True,
because the value of x is equal to 10
Example
Console .WriteLine(
10==15); // returns
False, because 10 is not equal to 15
Real Life Example
Let's think of a "real life example" where we need to find out if a person is old enough to
vote.
In the example below, we use the >= comparison operator to find out if
the age (25) is greater than OR
equal to the voting age limit, which is set to 18:
Example
int myAge=25;
int votingAge=18;
Console .WriteLine(
myAge>=votingAge);
Cool, right? An even better approach (since we are on a roll now), would be to wrap the code above in an
if...else statement, so we can perform different actions depending on the
result:
Example
Output "Old enough to vote!" if myAge is
greater than or equal to 18.
Otherwise output "Not old enough
to vote.":
int myAge=25;
int votingAge=18;
if (myAge>=votingAge)
{
Console .WriteLine(
"Old enough to vote!");
}
else
{
Console .WriteLine(
"20 is greater than 18");
}
The boolean value of an expression is the basis for all C# comparisons and conditions.
You will learn more about conditions (if...else) in the next chapter.