C# Programming for Beginners (2015)
Chapter 2: Data Types and Operators
Every program needs to store some data and perform some functionality on the data. In C#, data is stored in data types, and operations are performed on the data using C# operators. In this chapter, we will take a look at some of the most commonly used data types, as well as the operators which operate on the data to help us achieve some meaningful functionality.
Contents
· Data types in C#
· Operators in C#
1- Data Types in C#
The following table demonstrates the usage of each data type along with the range of data that each data type can store.
Type |
Represents |
Range |
bool |
Boolean value |
True or False |
byte |
8-bit unsigned integer |
0 to 255 |
char |
16-bit Unicode character |
U +0000 to U +ffff |
decimal |
128-bit precise decimal values with 28-29 significant digits |
(-7.9 x 1028 to 7.9 x 1028) / 100 to 28 |
double |
64-bit double-precision floating point type |
(+/-)5.0 x 10-324 to (+/-)1.7 x 10308 |
float |
32-bit single-precision floating point type |
-3.4 x 1038 to + 3.4 x 1038 |
int |
32-bit signed integer type |
-2,147,483,648 to 2,147,483,647 |
long |
64-bit signed integer type |
-923,372,036,854,775,808 to 9,223,372,036,854,775,807 |
sbyte |
8-bit signed integer type |
-128 to 127 |
short |
16-bit signed integer type |
-32,768 to 32,767 |
uint |
32-bit unsigned integer type |
0 to 4,294,967,295 |
ulong |
64-bit unsigned integer type |
0 to 18,446,744,073,709,551,615 |
ushort |
16-bit unsigned integer type |
0 to 65,535 |
Table 1.0
2- Operators in C#
In this section we are going to take a bird’s eye view of three types of operators in C#: Arithmetic Operators, Relational Operators, and Logical Operators.
· Arithmetic Operators
Arithmetic operators in C# perform the same functionality as they do in real life. These operators are used to perform various mathematical functions in C#. Arithmetic operators can only be applied to the operands of numeric and char data types. Table 1.1 enlists these operators along with their functionalities.
Operator |
What they do |
+ |
Addition and unary plus |
- |
Subtraction and unary minus |
* |
Multiplication |
/ |
Division |
% |
Modulus |
++ |
Increment a number |
+= |
Increment and assign |
-= |
Decrement and assign |
*= |
Multiply and assign |
/= |
Divide and Assign |
%= |
Modulus and Assign |
-- |
Decrement a Number |
· Table 1.1
Have a look at the first example of this chapter to see some of the arithmetic operators in action.
Example1:
using System;
namespaceMyFirstApplication
{
class Program
{
static void Main(string[] args)
{
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
int sub = num1 - num2;
intmulti = num1 * num2;
int division = num1 / num2;
int mod = num1 % num2;
Console.WriteLine( "Addition:"+sum+"\nSubtraction:"+sub+
"\nMultiplication:"+multi+"\nDivision"+
division+"\nModulus:"+mod);
Console.Read();
}
}
}
Here in Example1, we have declared two integer type variables, num1 and num2. These two variables store integers 10 and 5, respectively. Next, we have declared five integer type variables that store the sum, minus, multiplication, division, and modulus of num1 and number2. Finally, these variables have been printed on the console screen using “Console.WriteLine”. The output of the code in Example1 is as follows:
Output 1:
Addition:15
Subtraction:5
Multiplication:50
Division: 2
Modulus:0
· Relational Operators
In C#, relational operators are used for comparing and ordering two operands. Table 1.2 enlists C# relational operators along with their functionalities.
Operators |
What they do |
== |
Compare for equality |
!= |
Compare for inequality |
> |
Compare if operator on the left is greater |
< |
Compare if operator on the left is smaller |
>= |
Compare if operator on the left is greater or equal to |
<= |
Compare if operator on the left is smaller or equal to |
Table 1.2
Example 2 demonstrates the usage of relational operators in C#.
Example2:
using System;
namespaceMyFirstApplication
{
class Program
{
static void Main(string[] args)
{
int num1 = 10;
int num2 = 20;
if (num1 == num2)
{
Console.WriteLine("Num1 is equal to Num2");
}
if (num1 != num2)
{
Console.WriteLine("Num1 is not equal to Num2");
}
if (num1 > num2)
{
Console.WriteLine("Num1 is greater than Num2");
}
if (num1 < num2)
{
Console.WriteLine("Num1 is smaller than Num2");
}
if (num1 >= num2)
{
Console.WriteLine("Num1 is greater than or equal to Num2");
}
if (num1 <= num2)
{
Console.WriteLine("Num1 is smaller than or equal to Num2");
}
Console.Read();
}
}
}
In Example2, two integer type variables, num1 and num2, have been instantiated with some values; then, in order to compare them, all the relational operators have been sequentially applied to them. Do not worry if you are unable to understand the “if” statement followed by the opening and closing round brackets. We will discuss that in detail in the next chapter. The output of the code in Example2 is as follows:
Output2:
Num1 is not equal to Num2
Num1 is smaller than Num2
Num1 is smaller than or equal to Num2
1- Logical Operators
Logical operators operate only on Boolean operands. The result of logical operation is another Boolean value. Table 1.3 lists C# logical operators:
Operator |
What they do? |
& |
Logical AND |
| |
Logical OR |
^ |
XOR (Exclusive OR) |
|| |
Short Circuit OR |
&& |
Short Circuit AND |
! |
Unary NOT |
&= |
AND Followed by Assignment |
|= |
OR Followed by Assignment |
^= |
XOR Followed by Assignment |
== |
Equal |
!= |
Not Equal |
?: |
Ternary operator used for If then else |
Table 1.3
Exercise 2
Task:
Initialize three integers with random numbers. If the first integer is equal to the second integer and both the first and second integers are greater than the third integer, multiply the three. Otherwise, add the three. Display the result on console.
Solution
using System;
namespaceMyFirstApplication
{
class Program
{
static void Main(string[] args)
{
int num1 = 20;
int num2 = 20;
int num3 = 5;
if( (num1 == num2) && (num1 >num3) && num2 > num3)
{
int result = num1* num2* num3;
Console.WriteLine(result);
}
else
{
int result = num1 +num2 + num3;
Console.WriteLine(result);
}
Console.Read();
}
}
}