Arithmetic operators are used to perform common mathematical operations.
The arithmetic operators perform arithmetic operations on all the numeric type operands such as: sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, float
, double
, and decimal
.
These are useful to perform basic arithmetic calculations like addition, subtraction, division, etc., based on our requirements.
The arithmetic operators in C# is listed as follows:
Operator | Name | Description |
---|---|---|
+ | Addition | Adds together two values. For example, x+y . |
- | Subtraction | Subtracts one value from another. For example, x-y . |
* | Multiplication | Multiplies two values. For example, x*y . |
/ | Division | Divides one value by another. For example, x/y . |
% | Modulus | Returns the division remainder. For example, x%y . |
Note: The exception is the unary negative operator (-
) which serves to indicate that a value is negative rather than positive. This contrasts with the subtraction operator (-
) which takes two operands (i.e. one value to be subtracted from another).
For example:
int x = -20;// Unary - operator used to assign -20 to a variable named x
x = y - z; // Subtraction operator. Subtracts z from y
*** Note that multiple operators may be used in a single expression:
x = y * 20 + z - 4 / 2;
Whilst the above code is perfectly valid it is important to be aware that C# does not evaluate the expression from left to right or right to left, but rather in an order specified by the precedence of the various operators.
C# Operator precedence is an important topic to understand since it impacts the result of a calculation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int result;
int x = 4, y = 2;
// Addition (+)
result = (x + y);
Console.WriteLine("Addition Operator: " + result);
// Subtraction (-)
result = (x - y);
Console.WriteLine("Subtraction Operator: " + result);
// Multiplication (*)
result = (x * y);
Console.WriteLine("Multiplication Operator: " + result);
// Division (/)
result = (x / y);
Console.WriteLine("Division Operator: " + result);
// Modulo (%)
result = (x % y);
Console.WriteLine("Modulo Operator: " + result);
Console.ReadLine();
}
}
}
Output:
++
, --
respectively.Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int x = 0, y = 0;
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(x++); // Outputs 0; x is now 1
Console.WriteLine(++y); // Outputs 1; y is now 1
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadLine();
}
}
}
Output:
At runtime, arithmetic operations on integral types can overflow.
For example, decrementing the minimum possible int value results in the maximum possible int value:
int a = int.MinValue;
a--;
Console.WriteLine (a == int.MaxValue); // True
The checked
operator tells the runtime to generate an OverflowException
rather than overflowing silently in case of overflow.
The checked
operator affects expressions with the ++
, --
, +
, -
, *
, /
, and explicit conversion operators between integral types.
The checked
operator has no effect on the double and float types.
The checked
operator has no effect on the decimal type which is always checked.
checked
can be used around either an expression or a statement block.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int a = 2;
int b = 40;
int c = checked(a * b); // Checks just the expression.
// Checks all expressions in statement block.
checked
{
c = a * b;
}
Console.WriteLine(c);
Console.ReadLine();
}
}
}
Output:
We can check all arithmetic overflow for a program by compiling with the /checked+ command-line switch.
To disable overflow checking for specific expressions or statements, use the unchecked
operator.
For example, the following code will not throw exceptions-even if compiled with /checked+
:
int x = int.MaxValue;
int y = unchecked (x + 1);
unchecked { int z = x + 1; }
Regardless of the /checked
compiler switch, expressions evaluated at compile time are always overflow-checked-unless we apply the unchecked
operator:
int x = int.MaxValue + 1; // Compile-time error
int y = unchecked (int.MaxValue + 1); // No errors