C# Basics – Variables and Data Types – Part 4

We explore how variables work, in C# programming language, in this tutorial.

Andraz Krzisnik
C# Basics – Variables and Data Types...

Variables and Data Types

Software works with data that’s stored in the memory. All this data, to be usable, needs to be stored in some defined space. We name these spaces variables. Variables can have their values changed, overwriting the last one it held.

There are many different data types so, we can create different variables, such as integers and strings.

Every variable needs a name, so we can call it throughout our code.

string s = "Here is some text";
int i = 35;

When we create our new variable, we also need to declare it. This means we assign it a value.


When we have a variable that needs to be denoted as integer, we type int in front of the variable name, so this variable can hold only integers and no other data type values.

We can still normally use operations such as summing, subtracting, multiplying. But when it comes to dividing there is something you should know.

Since the result of each operation will be an integer as well, these results won’t be accurate, because all numbers beyond the decimal point will be erased.

So for example if we tried to divide 5 by 3, our result will be 1. There is also one more operator that can come in handy if we need more accurate results than that.

The % operator will give us the remainder. So basically if we try 5 % 3, the result will be 2, since 5 / 3 is 1 and the remainder is 2.

int a = 5 + 3;
int b = 5 - 3;
int c = 5 * 3;
int d = 5 / 3;
int f = 5 % 3;

MessageBox.Show("Sum: " + a.ToString());
MessageBox.Show("Subtract: " + b.ToString());
MessageBox.Show("Multiply: " + c.ToString());
MessageBox.Show("Divide: " + d.ToString());
MessageBox.Show("Remainder: " + f.ToString());

Doubles – Real Numbers

To create a variable which will hold a real number we need to type double before the name of the variable. These kind of variables can hold numbers with decimal point, so it can be much more precise than integer type variable.

double r = 2.667;

Example above will display a window with showing us a real number.

Math Class – Functions

We can find functions for more complex operations like trigonomic functions, rounding, square root and more in this class. When we call it we have to write down class name first followed by a dot (.) and finally select the function from this class we want to use.



  • PI – number ?


  • Abs – absolute value
  • Acos – arcus cosine – angle (in radians)
  • Asin – arcus sine
  • Atan – arcus tangent
  • Cos – cosine of an angle
  • Max – returns the largest of two numbers
  • Min – returns the lowest of two numbers
  • Pow – exponentials (first number is the base, second is the exponent)
  • Round – rounds to the closest integer number
  • Sqrt – square root
  • Tan – tangent
  • Truncate – returns a whole number by cutting decimals off

Converting Between Data Types

There may be many occurrences when you will need to convert a value from one data type to another. Some can be converted automatically, another are converted with code.

Following example will show you how to convert a number value, of type integer to a double.

int a = 8;
double converted = (double)a;

Here is another example, this one shows how to convert string type to integer.

string n = "5";
int m = Convert.ToInt32(n);

How to convert from string to a double

string j = "5,6";
double k = Convert.ToDouble(j);
Download Examples Next Chapter
Show Comments (0)


Related Articles

C# Basics

C# Basics – Constructors – Part 12

Constructors Sometimes we want to create an object and set some predetermined state to it. Basically, you would pass the properties’ values as an argument to an object when you...

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: Contrast Stretching with Normalization

This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...

Posted on by Andraz Krzisnik